Python Comments

Ka Kavitha V Updated 03 Oct 2026
5 min read ·Lesson 4 of 23

Comments

Comments are notes that you add inside your code. They help developers understand what the code is doing, make programs easier to read, and allow you to temporarily disable parts of the code during testing or debugging.

Python completely ignores comments while executing a program, so they do not affect the output.

Why Use Comments?

Code tells the computer what to do. Comments tell people why it is done that way. Comments are useful for:

  • Explaining intent — why a particular approach or value was chosen
  • Improving readability — helping teammates (and your future self) understand the code quickly
  • Debugging — temporarily switching off lines of code without deleting them
  • Leaving reminders — marking work that still needs to be done, such as # TODO: handle empty input

Single-Line Comments

In Python, a comment begins with the hash symbol #. Everything after # on that line is treated as a comment and skipped during execution.

Example

# This message explains what the next line does
print("Python comments are useful")

Expected output:

Python comments are useful

The first line is ignored completely. Only the print() statement runs.

Inline Comments

A comment can also be placed on the same line as a statement. Python executes the code before the # and ignores everything after it.

Example

print("Welcome to coding")  # This text will not be executed

Expected output:

Welcome to coding

Inline comments are helpful for briefly describing a specific line of code:

tax_rate = 0.18      # GST rate applied to all products
price = 500
total = price + price * tax_rate
print(total)

Expected output:

590.0

Style tip: PEP 8, Python's official style guide, recommends at least two spaces before the # of an inline comment and one space after it.

A # Inside a String Is Not a Comment

The # symbol only starts a comment when it appears outside a string. Inside quotation marks, it is just a normal character:

print("Use #python to find posts")  # Only this part is a comment

Expected output:

Use #python to find posts

Using Comments to Disable Code

Comments can stop certain lines of code from running without deleting them. This is commonly done while testing or debugging — for example, to check whether a particular line is causing a problem.

Example

# print("This line is temporarily disabled")
print("This line will execute normally")

Expected output:

This line will execute normally

Only the uncommented line runs. To re-enable the disabled line, simply remove the #.

Tip: Most code editors can comment or uncomment several selected lines at once using a shortcut such as Ctrl + / (Windows/Linux) or Cmd + / (macOS).

Multi-Line Comments

Python does not have a dedicated syntax for multi-line comments, unlike languages such as C or Java, which use /* ... */. However, there are two common ways to write comments that span several lines.

Place a # at the beginning of each line.

# This block of text
# explains the purpose
# of the program below
print("Learning Python comments")

Expected output:

Learning Python comments

This is the recommended approach, because every line is clearly a real comment that Python ignores.

Method 2: Multi-Line Strings

You can also use triple quotes (""" """ or ''' ''') to write text across several lines.

"""
This is a multi-line note
used for documentation
or explanation purposes
"""
print("Python ignores the text above")

Expected output:

Python ignores the text above

How This Works

Technically, triple-quoted text is not a comment — it is a string literal. Python creates the string, but because it is not assigned to a variable or used anywhere, it has no effect on the program. That is why it appears to be ignored.

Although this method works, it is mainly intended for docstrings, not for regular comments.

Docstrings

A docstring (documentation string) is a triple-quoted string placed as the first statement inside a function, class, or module. Unlike an ordinary comment, a docstring is stored by Python and can be read by tools and by the built-in help() function.

def add(a, b):
    """Return the sum of a and b."""
    return a + b

print(add(2, 3))
print(add.__doc__)

Expected output:

5
Return the sum of a and b.
  • """Return the sum of a and b.""" is the docstring of the add() function.
  • add.__doc__ retrieves the docstring, proving that Python kept it.

Comments vs Docstrings

FeatureComment (#)Docstring (""" """)
PurposeExplain how or why the code worksDescribe what a function, class, or module does
Kept by Python at runtimeNoYes, in the __doc__ attribute
PlacementAnywhereFirst statement in a function, class, or module
Readable with help()NoYes

Writing Good Comments

Good comments explain why, not what. The code already shows what it does.

Not helpful — repeats the code:

count = count + 1  # Add 1 to count

Helpful — explains the reason:

count = count + 1  # Include the header row in the total

Common Mistakes

  • Over-commenting: Writing a comment for every obvious line makes code harder to read, not easier.
  • Outdated comments: If you change the code, update its comments. A wrong comment is worse than no comment.
  • Using triple-quoted strings as comments everywhere: Use # for comments and keep triple quotes for docstrings.
  • Leaving large blocks of disabled code: Commenting out code is useful during debugging, but remove dead code before finalizing your program.

Best Practices

  • Start comments with # followed by a single space: # Like this.
  • Keep comments short and clear.
  • Write docstrings for functions and classes that other people will use.
  • Prefer clear variable and function names; good names reduce the need for comments.
  • Python Syntax — the rules that define valid Python code
  • Python Functions — where docstrings are most commonly used
  • Python Variables — choosing names that make code self-explanatory

0 Comments

Reviewed before they appear

No comments yet.

Python
Ask about this post
AI Ask about this post

Ask questions about Python Comments and get answers drawn from it.

Signed-in readers only.