Python Comments Explained (Single-Line and Multi-Line)
Comments help you write code that is easier to read and understand.
In Python, comments are notes for humans. Python ignores them when it runs your program. This makes comments useful for explaining what your code does, why it exists, or what to watch out for.
This page shows you how to write comments in Python, when to use them, and how to avoid confusing comments with triple-quoted strings.
Quick example
# Single-line comment
name = "Maya" # Inline comment
"""
This is a multi-line string.
It is often used like a comment,
but it is not a real comment.
"""
print(name)
Important: Use # for real comments. Triple quotes create a string, not a true comment.
What this page helps with
- Understand what a comment is
- Write single-line comments with
# - Understand inline comments
- Learn the common "multi-line comment" style
- Avoid confusing comments with strings
What a comment is in Python
A comment is text that Python ignores when running code.
Comments are useful because they help people reading the code:
- Explain what a step does
- Explain why code was written a certain way
- Add a short note before something tricky
- Make code easier to come back to later
Comments do not change the result of your program.
Example:
# Store the user's name
name = "Maya"
print(name)
When Python runs this code, it ignores the comment and only runs the real code.
How to write a single-line comment
To write a comment in Python, start the line with #.
Everything after # on that line is ignored.
# This is a comment
print("Hello")
You can also use comments to explain the next step:
# Ask the user for their age
age = input("Enter your age: ")
Single-line comments are best for short, clear notes.
Good comments are:
- Short
- Specific
- Easy to understand
Inline comments
An inline comment is written after code on the same line.
name = "Maya" # Save the user's name
This can be helpful, but only when the comment adds useful context.
Use inline comments carefully:
- Good when the reason is not obvious
- Bad when they only repeat the code
- Too many can make code harder to read
Good inline comment
total = price * 1.2 # Add 20% tax
Not very helpful
name = "Maya" # Set name to Maya
The second comment does not add much. The code already makes that clear.
If you are still learning basic code structure, this fits well with Python syntax basics and Python indentation rules.
Multi-line comments in Python
Python does not have a special multi-line comment syntax.
The normal way to write a comment across several lines is to put # at the start of each line:
# This program asks for a name,
# stores it in a variable,
# and then prints a greeting.
name = input("Enter your name: ")
print("Hello,", name)
This is the standard way to write multi-line comments in Python.
Beginners often see triple quotes used like this:
"""
This looks like a multi-line comment,
but Python treats it as a string.
"""
print("Hello")
That style is common, but it is not a real comment.
Triple quotes vs real comments
Triple quotes create a multi-line string.
text = """This is
a multi-line
string."""
print(text)
Output:
This is
a multi-line
string.
That means triple-quoted text is different from a real comment.
Real comment
# Python ignores this line
print("Hello")
Triple-quoted string
"""Python reads this as a string."""
print("Hello")
In some places, triple-quoted strings are used as docstrings. A docstring is a special string used to describe a module, function, class, or method.
Example:
def greet(name):
"""Return a friendly greeting."""
return f"Hello, {name}"
Here, the triple-quoted text is not just a note. It is documentation attached to the function.
If you want to learn more about functions, see Python functions explained. If you are not sure what a module is, see what is a module in Python.
When comments are helpful
Comments are most useful when they explain something the code does not clearly say on its own.
Helpful comments can:
- Explain why something is done
- Mark important steps for beginners
- Add a short note before more complex logic
- Clarify unusual behavior or edge cases
Example:
# Use 1-based numbering because the report format starts at 1
report_number = user_index + 1
This is useful because the reason may not be obvious just from reading the code.
Another example:
# Skip empty lines to avoid counting blank input
if line != "":
count += 1
When comments are not helpful
Comments can also make code worse if they are unnecessary or outdated.
Avoid comments like these:
- Comments that repeat obvious code
- Comments that are no longer true
- Very long comments that interrupt reading
- Comments used instead of clear variable names
Example of an unhelpful comment:
# Add 1 to count
count = count + 1
The code already says that.
A better improvement might be a clearer variable name or cleaner code.
You should also avoid using comments to explain confusing code when you could rewrite the code more clearly.
Best practices for beginners
If you are new to Python, these habits will help:
- Write comments in plain language
- Keep comments short
- Put comments close to the code they describe
- Update comments when the code changes
- Use comments to support readable code, not replace it
A good goal is this:
- Write code that is clear on its own
- Add comments only where they truly help
For example:
# Convert minutes to seconds before saving
seconds = minutes * 60
This comment gives useful context without being too long.
You may also notice that Python code depends on clean layout and structure. That is one reason Python keywords and indentation rules matter when writing readable code.
Common mistakes
These are common comment-related mistakes beginners make:
- Using triple quotes and thinking they are always comments
- Writing comments that only repeat the code
- Leaving old comments after changing the code
- Putting too many inline comments on one line
- Mixing comments with code in a way that hurts readability
Example of cluttered code:
name = "Maya" # user's name # stored as text # printed below
print(name) # show name
This works, but it is harder to read than necessary.
A cleaner version:
# Store the user's name
name = "Maya"
print(name)
If you want to test your script while learning, you can run it from the terminal:
python your_script.py
You may also see beginners explore Python with:
help(print)
dir(__builtins__)
These are not comment tools, but they can help you learn how Python works.
FAQ
How do you write a comment in Python?
Use # followed by your note. Python ignores the rest of that line.
Does Python have multi-line comments?
Not as a separate syntax. The usual approach is to write # on each line.
Are triple quotes comments in Python?
No. Triple quotes create a string. They are often used for docstrings.
What is an inline comment?
It is a comment written after code on the same line.
Should beginners use many comments?
Use comments when they make code clearer, but do not comment every obvious line.