Python Comments Explained — Single-Line, Multi-Line and Best Practices
Every line of code you write today is a mystery you'll need to solve tomorrow. That sounds dramatic, but ask any developer who's opened a file they wrote six months ago — without comments, even your own code can look like a foreign language. Comments are the single cheapest investment you can make to keep your code readable, maintainable, and friendly to every person who touches it after you.
Python runs fast. It processes your instructions line by line, but it skips comments entirely — they exist purely for people, not machines. This solves a real problem: code tells the computer WHAT to do, but it rarely explains WHY a decision was made, what a tricky block is trying to achieve, or what a number like 0.0875 actually represents (is that a tax rate? a discount? who knows). Comments fill that gap.
By the end of this article you'll know exactly how to write single-line comments, how to fake multi-line comments the Pythonic way, how to use docstrings to document functions properly, and — most importantly — when to write a comment versus when to just write cleaner code. You'll also see the mistakes that trip up beginners and the answers that impress interviewers.
Single-Line Comments — The Workhorse You'll Use Every Day
A single-line comment in Python starts with the hash symbol #. Everything after that # on the same line is ignored by Python. That's the whole rule. There's nothing to install, no special mode to activate — just type # and write your note.
You can put a comment on its own line above the code it describes, which is called a 'block comment'. Or you can put it at the end of a line of code, which is called an 'inline comment'. Both are valid, but block comments are usually easier to read because they don't crowd the code.
Here's the key habit to build early: comment the WHY, not the WHAT. If you write total_price = price + tax and then comment # adds price and tax, that comment adds zero value — anyone can see that. But if you comment # tax is added here because EU law requires it to appear in the final total, now you've explained something the code can't explain itself. That's what comments are for.
# This program calculates the final price a customer pays, including sales tax. # Written by: TheCodeForge Team item_price = 49.99 # Price of the item in US dollars tax_rate = 0.0875 # 8.75% sales tax — required by local tax law # Multiply the item price by the tax rate to get the tax amount tax_amount = item_price * tax_rate # Add the original price and the tax to get the total the customer owes total_price = item_price + tax_amount # Round to 2 decimal places so we get a clean dollar amount (e.g. 54.37, not 54.374375) final_price = round(total_price, 2) print("Item price: $", item_price) print("Tax amount: $", round(tax_amount, 2)) print("Total price: $", final_price)
Tax amount: $ 4.37
Total price: $ 54.36
Multi-Line Comments — What Python Actually Does (and Doesn't Have)
Here's something that surprises beginners: Python has no official multi-line comment syntax. Languages like Java and JavaScript have / ... / block comments, but Python never added an equivalent. So how do Python developers write longer notes that span multiple lines?
The answer is simple — you just start each line with its own #. Stack as many lines as you need, each prefixed with a #. Python treats each one as a separate single-line comment, but visually they read as a block. This is the officially recommended approach in Python's own style guide (PEP 8).
You'll also see beginners use triple-quoted strings (''' or """) as a workaround for multi-line comments. A string that isn't assigned to a variable and isn't attached to a function is technically just a value Python evaluates and immediately throws away — so it behaves like a comment in practice. However, this is not a true comment, and there's one important exception: when a triple-quoted string appears as the very first statement inside a function, class, or module, it becomes a docstring — a documented description that tools and IDEs can actually read. More on that in the next section.
# ───────────────────────────────────────────── # APPROACH 1 — The Pythonic Way (Recommended) # Stack multiple single-line comments. # This is what PEP 8 (Python's style guide) recommends. # ───────────────────────────────────────────── # This function converts a temperature from Celsius to Fahrenheit. # Formula: F = (C × 9/5) + 32 # This conversion is needed because our US-based clients expect # temperatures displayed in Fahrenheit, while our database stores # all values in Celsius for international consistency. def celsius_to_fahrenheit(celsius_temp): fahrenheit_temp = (celsius_temp * 9 / 5) + 32 return fahrenheit_temp # ───────────────────────────────────────────── # APPROACH 2 — Triple-quoted string used as a comment # This works but is not technically a comment. # Only use this for docstrings (see next section). # ───────────────────────────────────────────── """ This block is a string that gets evaluated and discarded. Python doesn't execute it as code, and it has no variable to store it, so it effectively acts like a comment — but this is a workaround, not an official language feature. """ boiling_point_celsius = 100 boiling_point_fahrenheit = celsius_to_fahrenheit(boiling_point_celsius) print("Boiling point in Celsius: ", boiling_point_celsius, "°C") print("Boiling point in Fahrenheit:", boiling_point_fahrenheit, "°F")
Boiling point in Fahrenheit: 212.0 °F
Docstrings — Comments That Your Code Can Actually Read
A docstring is a special triple-quoted string placed as the very first statement in a function, class, or module. It's Python's official way of documenting what a piece of code does — and unlike a regular comment, Python stores the docstring and makes it accessible at runtime via the __doc__ attribute and the built-in help() function.
Think of a regular comment as a sticky note on your desk — helpful to you, invisible to everyone else. A docstring is like a label on a product — it's built into the thing itself and anyone can read it just by inspecting the object.
Docstrings follow a convention: the first line is a short, one-sentence summary of what the function does. If you need more detail, add a blank line, then a longer description, then document the parameters and return value. You don't have to memorise a rigid format right now — just know that the first line matters most and should be a clear, complete sentence.
For beginners, the most important takeaway is this: every function you write should have a docstring. It takes ten seconds, and it pays back tenfold.
def calculate_discount_price(original_price, discount_percent): """ Calculate the price of an item after applying a percentage discount. This function takes the original price and a discount percentage, computes the amount saved, and returns the final discounted price rounded to two decimal places. Parameters: original_price (float): The full price of the item before discount. discount_percent (float): The discount as a percentage (e.g. 20 for 20%). Returns: float: The price the customer actually pays after the discount. Example: calculate_discount_price(80.00, 25) returns 60.0 """ # Convert the percentage to a decimal (e.g. 20% becomes 0.20) discount_as_decimal = discount_percent / 100 # Calculate how much money is being taken off amount_saved = original_price * discount_as_decimal # Subtract the saving from the original price to get what the customer pays discounted_price = original_price - amount_saved return round(discounted_price, 2) # ── Demonstrating that the docstring is stored and readable at runtime ── print("--- Running the function ---") original = 120.00 discount = 30 # 30% off final_cost = calculate_discount_price(original, discount) print(f"Original price: ${original}") print(f"Discount applied: {discount}%") print(f"You pay: ${final_cost}") print("\n--- Reading the docstring at runtime ---") print(calculate_discount_price.__doc__)
Original price: $120.0
Discount applied: 30%
You pay: $84.0
--- Reading the docstring at runtime ---
Calculate the price of an item after applying a percentage discount.
This function takes the original price and a discount percentage,
computes the amount saved, and returns the final discounted price
rounded to two decimal places.
Parameters:
original_price (float): The full price of the item before discount.
discount_percent (float): The discount as a percentage (e.g. 20 for 20%).
Returns:
float: The price the customer actually pays after the discount.
Example:
calculate_discount_price(80.00, 25) returns 60.0
| Feature / Aspect | Single-Line Comment (#) | Docstring (""") |
|---|---|---|
| Syntax | # your note here | """ your note here """ |
| Spans multiple lines? | Only by stacking multiple # lines | Yes, natively spans as many lines as needed |
| Stored in memory at runtime? | No — completely ignored by Python | Yes — stored in object's __doc__ attribute |
| Readable by help() or IDEs? | No | Yes — IDEs show docstrings as hover tooltips |
| Where is it used? | Anywhere in code — above or beside any line | First statement inside a function, class, or module |
| Recommended by PEP 8? | Yes, for inline and block notes | Yes, for all public functions and classes |
| Can it affect program behaviour? | Never | Only if misplaced — a stray triple-string is evaluated as an expression |
🎯 Key Takeaways
- Every comment you write is for a human, not Python — the interpreter ignores every # comment completely and never executes it.
- Python has no official multi-line comment syntax — stack multiple # lines for block comments; triple-quoted strings are a workaround, not a language feature.
- A docstring is not just a comment — it's stored as the __doc__ attribute of a function or class and can be read at runtime by help(), IDEs, and documentation tools.
- Comment the WHY, not the WHAT — if your comment just re-states what the code clearly does, delete it; comments earn their place by explaining intent, constraints, and non-obvious decisions.
⚠ Common Mistakes to Avoid
- ✕Mistake 1: Commenting WHAT instead of WHY — Writing '# adds 1 to counter' above 'counter = counter + 1' adds zero value; anyone can read the code. The fix: ask yourself 'would a new developer know WHY this is done without my comment?' If the code is self-explanatory, skip the comment entirely. If there's a business rule, edge case, or non-obvious decision, explain that instead.
- ✕Mistake 2: Using triple-quoted strings as comments inside function bodies — A triple-quoted string that isn't the first statement of a function is still a string expression that Python evaluates. It won't crash your program, but it wastes a tiny amount of processing, and more importantly it can confuse other developers who expect triple quotes to mean 'docstring'. Fix: always use # for mid-function notes and reserve triple quotes for the opening docstring only.
- ✕Mistake 3: Commenting out dead code and leaving it forever — Beginners often comment out old code 'just in case I need it later' and commit it to the project. After weeks this creates files full of commented-out clutter that nobody dares delete. The fix: use version control (like Git). If you might need old code later, that's what commit history is for — delete the code confidently and know you can retrieve it from Git any time.
Interview Questions on This Topic
- QWhat is the difference between a comment and a docstring in Python, and how does Python treat each one at runtime?
- QDoes Python have a built-in multi-line comment syntax? If not, what are the two approaches developers use, and which one does PEP 8 recommend?
- QIf I write a triple-quoted string at the start of a function versus in the middle of a function, does Python treat them the same way? Explain what happens in each case.
Frequently Asked Questions
Does Python have multi-line comments like /* */ in other languages?
No, Python has no dedicated multi-line comment syntax. The standard approach is to prefix each line with a # symbol and stack them vertically. Some developers use triple-quoted strings as a workaround, but these are technically string expressions, not true comments, so the recommended PEP 8 approach is always stacked # lines.
What is the difference between a comment and a docstring in Python?
A comment (starting with #) is completely ignored by Python at runtime and exists only for human readers. A docstring (a triple-quoted string as the first statement of a function, class, or module) is stored in memory as the object's __doc__ attribute and can be accessed programmatically using help() or by reading __doc__ directly. Comments document individual lines of logic; docstrings document the purpose and interface of an entire function or class.
Should I comment every single line of Python code I write?
No — over-commenting is just as harmful as under-commenting. Well-named variables and functions often make code self-documenting. A comment earns its place when it explains WHY a decision was made, documents a business rule, warns about a non-obvious side effect, or clarifies a complex algorithm. If the comment just re-states what the code obviously does, it's noise — remove it.
Written and reviewed by senior developers with real-world experience across enterprise, startup and open-source projects. Every article on TheCodeForge is written to be clear, accurate and genuinely useful — not just SEO filler.