Skip to main content

How to Write a Comment in Python

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A comment is a note in your code for the people who read it. Python skips it when the program runs. In Python, a comment starts with #, and this guide writes one on a line of its own and one after a line of code, on Python 3.15.2.

Syntax​

Syntax
# <note>
<code> # <note>
PartWhat it means
#Starts the comment. Everything after it, up to the end of the line, is skipped
<note>Your text. It can say anything
<code> # <note>A comment can also follow code on the same line. The code still runs

The Python tutorial says: "Comments in Python start with the hash character, #, and extend to the end of the physical line. A comment may appear at the start of a line or following whitespace or code, but not within a string literal."

The Example​

The script has a comment on a line of its own, a comment after the line that sets a price, and a # inside quotes. It prints the price and the quoted text.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

comments.py
# This line is a comment, so Python skips it.
price = 4 # A comment can also follow code.

print(price)
print("# This is text, not a comment")

The comments.py script in a macOS-style code window: a comment on the first line, price set to 4 with a comment after it, then prints of price and of a string that starts with a hash.

  • The first line is only a comment, so it does nothing when the script runs.
  • price = 4 # A comment can also follow code. runs the code before the # and skips the rest of the line.
  • "# This is text, not a comment" has its # inside quotes, so it's part of the text.

Step 2: Run It​

Terminal
uv run python comments.py
Output
4
# This is text, not a comment

The terminal after uv run python comments.py: 4, then # This is text, not a comment.

Neither comment appears in the output, and price is 4, so the code in front of the second comment ran. The # inside quotes printed as text: the tutorial puts it as "A hash character within a string literal is just a hash character." The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Starting a comment with //, as in some other languagesThe script stops before it runs a line: SyntaxError: invalid syntax, pointing at the //Start the comment with #

FAQ​

How do I switch off several lines?​

Put a # at the start of each line. In a script that prints "This line runs" and then has two more print lines that start with #, only This line runs appeared.

Conclusion​

# turned the first line, and the end of the price line, into notes that Python skipped, while the # inside quotes stayed part of the printed text.

Additional Resources​