iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
In Python, write # outside a string to start a comment; it continues to the end of that physical line and is ignored as an instruction by Python. A useful comment gives a future reader context the code does not show. An obvious or outdated comment can instead waste time or mislead.
How do I comment in Python?
Put # before a note on its own line, or after a statement for a short inline note:
# A standalone comment
count = 3 # An end-of-line comment
message = "Use # in this displayed example" # The hash in the string is not a comment
Outside a string literal, the first # marks the comment; Python ignores the comment text through the end of the physical line. A hash inside quotes is part of the string instead. The Python tutorial demonstrates standalone comments, inline comments, and hashes inside strings in its informal introduction.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWhat does # do in Python?
Ordinary comments explain code to people; they are not program instructions and do not affect execution. The language reference says comments are ignored by the syntax. There is one useful advanced exception to the simple rule: a comment matching the encoding-declaration form in either of the first two source lines can specify the source encoding. Python uses UTF-8 by default if no such declaration is found. See the Python language reference for the exact rule.
#1 Best Overall
When should you add a comment?
Add a comment when a reader cannot reasonably infer an important reason, assumption, constraint, or decision from the code itself. The comment should contribute information rather than narrate the operation.
Explain non-obvious intent
For example, count += 1 already shows that the value increases by one. A note such as # Keep the zero-based offset aligned with the file header may be useful if that is genuinely the design reason and is not apparent elsewhere. PEP 8 illustrates this principle with a comment explaining a compensation that is not obvious from the statement.
Rank #2
Skip comments that only repeat the code
count += 1 # Add one to count
This note restates the operation without explaining why it matters. PEP 8 recommends using inline comments sparingly and keeping comments clear and current. These are style recommendations, not syntax requirements; see PEP 8.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsMake the reason easy to verify
A good comment is specific enough for someone to check against the surrounding code. Avoid vague notes such as “special case” when you can state which constraint or assumption requires the case. If the reason changes, revise or remove the comment along with the code.
What’s the difference between a comment and a docstring?
A # comment is a note placed near implementation details. A docstring is documentation associated with a module, class, or function, conventionally written as the first statement in that object’s definition. Use docstrings to describe what an object is for and, where applicable, its behavior, arguments, return value, side effects, exceptions, and restrictions. PEP 257 sets out these conventions: Docstring Conventions.
Do not treat any triple-quoted string as a general-purpose comment. Use a # comment for a nearby implementation note, and a docstring when documenting a module, class, or function according to the docstring convention.
How can comments help when you revisit code?
When returning to code later, look for comments that preserve context you would otherwise have to rediscover: a non-obvious constraint, the reason for a choice, or a relationship between this line and another part of the program. Read each note against the current code. If it merely repeats a visible operation, remove it; if it no longer describes the code, correct it.
Free tools Windows power users keep installed
One-click scans. No signup required.
PEP 8 warns, “Comments that contradict the code are worse than no comments,” and urges programmers to keep comments up to date when code changes. Studies of comments in selected Java and Python projects are not universal measures of how much comments improve comprehension: a 2021 case study examined commenting conventions in class comments, while a 2019 study reported classifier metrics for explanatory local comments in 2,000 GitHub projects written in Java and Python. Those findings describe particular datasets and methods, not a guaranteed benefit for every reader or codebase: 2021 study; 2019 study.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

