How to comment code in Python is a basic skill that makes code easier to read, explain, and temporarily disable when needed. Python relies on one main comment symbol, plus a few practical patterns depending on the task.
Single-line comments in Python
Single-line comments in Python use the # symbol before text or code. Everything after that symbol on the same line is ignored by the interpreter.
Example:
# This is a comment
print(“Hello”) # Comment after code
This format works well for short explanations, quick notes, and temporarily disabling one line of code.
How to comment out multiple lines
How to comment code in Python across several lines is usually done by placing # at the start of each line. That is the standard and most reliable approach.
Example:
# print(“First line”)
# print(“Second line”)
# print(“Third line”)
Most code editors can do this with a shortcut that adds or removes the comment symbol for all selected lines. After applying it, run the file again to confirm the code no longer executes.
Multi-line text and docstrings
Multi-line strings in quotes are sometimes mistaken for comments, but they are not the same thing. If such a block is not assigned to a variable or used by a function, it may look like a comment, but technically it is still a string literal.
For function, class, and module documentation, docstrings are the better choice:
def greet(name):
“””Return a greeting for the user.”””
return f”Hello, {name}”
Docstrings are meant to explain what code does, not to temporarily disable logic. If you need to comment out code, # is the safer option.
What to avoid when commenting code
How to comment code in Python without problems depends on more than syntax. Good comments add clarity; bad comments repeat the obvious or hide code that should be cleaned up.
- Do not leave outdated comments after changing the logic.
- Do not keep large blocks commented out for long periods if they should be removed or moved elsewhere.
- Do not use comments instead of clear variable and function names.
- Do not confuse a docstring with a comment for disabling code.
If a commented section still seems to run, check indentation, nested lines, and whether the same logic is being called somewhere else in the program.
The most practical approach for everyday use
The most practical way to comment code in Python is to use # for individual lines and docstrings only for documentation. That matches Python style, stays readable in editors, and avoids confusion between explanation and executable code.
A simple rule helps in daily work: use # when you want to disable code temporarily, use a docstring when you want to explain a function, class, or module, and delete code entirely when it is no longer needed.

