Open a program you wrote three months ago. Even if the code is correct, you may stare at a line and wonder why you wrote it that way. Now imagine a stranger reading it, or a new teammate on Monday morning. Code tells the computer what to do. Comments tell people why.
A comment is a note inside your code that Python ignores when the program runs. It does not change the result, but it changes how fast another person, or you in the future, can understand and fix the program. Good comments explain the reasons behind the code, flag the tricky parts, and mark work that is not finished. They are also a handy tool for switching a line off while you test.
In this tutorial, we explain how to write a comment in Python, and then go through single line comments, inline comments and the so called multi line comment. We show what Python really does with triple quotes, how docstrings differ from comments, and how to comment out code quickly. We finish with the style rules from PEP 8, examples of good and bad comments, and the common mistakes. Every example was run on Python 3.13.
A comment is text that the Python interpreter skips. You write it for human readers. Comments are used to explain what a block of code does, to describe why a choice was made, to leave a reminder, and to stop a line from running while you test.
A comment starts with a hash sign. Everything from the hash to the end of that line is ignored. The comment can sit on its own line, or after some code.
Only the print line ran. The comments left no trace and printed nothing.
People usually group comments into single line and multi line comments. The two groups are useful for learning, but the second one needs care, which is explained below.
A single line comment uses one hash sign and fits on one line.
Here is a single line comment in a working program:
An inline comment sits on the same line as the code, after it. Leave at least two spaces between the code and the hash, and one space after the hash. This is the PEP 8 rule, and it keeps the line readable.
If the hash sits inside quotes, Python treats it as part of the text.
Python has no special syntax for a multi line comment. There are two ways people write one, and they are not the same thing.
Way 1: several lines that each start with a hash. This is the real way, and the one that Python’s own style guide recommends. Most editors can add the hash to a whole block with one shortcut.
Way 2: a triple quoted string. Three single quotes or three double quotes can wrap text over many lines. Python does not treat this as a comment. It reads it as a string. Since nothing uses that string, Python throws it away, so it behaves like a comment.
Two mistakes break this. The first is curly quotes, which text copied from a web page or a word processor often contains. The second is opening with one kind of quote and closing with another. The marks must be straight, three of them, and the same at the start and the end. Here is what Python says to each broken version:
You can see the difference with the ast module, which shows how Python reads your code. A hash comment leaves nothing behind. A triple quoted line becomes a real statement in the code tree.
The first list has only the assignment, so the comment is gone. The second list has an extra Expr, which is the string. It is harmless and does nothing, but it is still code. And if you store the string in a variable, it is a normal value and not a comment at all:
For this reason, the better habit is to use hash lines for comments, and keep triple quotes for docstrings.
A docstring is a triple quoted string placed as the very first line inside a function, class or module. Python stores it, so tools and the help function can show it. It is the one case where triple quotes are the right choice.
The attribute __doc__ holds the docstring. Classes can have one too:
Longer functions can use several lines. Python keeps the line breaks, and removes the extra indent that comes from the code block (Python 3.13):
A normal comment never becomes a docstring. In the next example, the function has a comment but no docstring, so __doc__ is None:
| Feature | Comment with a hash | Docstring |
|---|---|---|
| Written for | People reading the code | People using the function, and tools |
| Kept by Python | No | Yes, in __doc__ |
| Where it goes | Anywhere | First line of a function, class or module |
| Marks | One hash per line | Three quotes at each end |
A very practical use of comments is to switch off a line without deleting it. Put a hash in front of the line, and Python skips it.
Most code editors have a shortcut that adds or removes the hash on every selected line. In VS Code and many other editors, it is Ctrl and the slash key on Windows and Linux, and Command and the slash key on a Mac. In PyCharm it is the same. IDLE uses Alt and 3 to comment a block, and Alt and 4 to remove it.
PEP 8 is the official Python style guide. Its advice on comments is short and worth following.
# Like this, and not #Like this.A comment should say something the code cannot say by itself. The best ones explain why.
Here is a bad comment. It repeats the code, so it adds nothing:
Here is a good one. It tells the reader a fact they could not see from the numbers:
| Write a comment to | Avoid a comment that |
|---|---|
| Explain why a choice was made | Repeats what the code already says |
| Warn about a tricky or surprising part | Is out of date and no longer true |
| Give the source of a formula or a rule | Excuses messy code, which you should clean up instead |
| Mark work for later | Hides old code that should be deleted |
A common habit is to mark unfinished work with the word TODO, so you can search for it later. Many editors highlight these.
You may see a line like the next one at the top of old files. It tells the interpreter the file encoding. In Python 3, files are read as UTF 8 by default, so you rarely need it. The line below is a special comment, but it is still a comment.
SyntaxError. Use straight quotes.| You want to | Write |
|---|---|
| A single line comment | # comment |
| A comment after code | code # comment |
| A multi line comment | One hash at the start of every line |
| A docstring | Triple quotes as the first line of a function, class or module |
| Stop a line from running | Put a hash in front of it |
| Read a docstring | name.__doc__ or help(name) |
Here is the full script used to produce every output on this page. Copy it into a file, run it with Python 3, and compare the results.
A good comment saves the next reader a minute of guessing, and that reader is often you. Use a hash for notes, triple quotes for docstrings, and keep the notes short and true. Run the script above, move the hash around, and see what changes. Then read a few well written open source Python files, and watch how their authors explain the hard parts.