Writing Comments in Python (Guide) - Real Python
Writing Comments in Python (Guide)
by Jaya Zhané Updated Dec 05, 2026 Reading time estimate 25m basics best-practicesTable of Contents
- Why Commenting Your Code Is So Important
- How to Write Comments in Python
- Python Commenting Best Practices
- Python Commenting Worst Practices
- How to Practice Commenting
- Conclusion
- Frequently Asked Questions
Recommended Course
Writing Comments in Python (29m)
When writing code in Python, it's important to make sure that others can understand your code . Giving variables obvious names, defining explicit functions , and organizing your code are all great ways to do this. Another way to make your code more readable is to write Python comments !
By the end of this tutorial, you'll understand that:
- A comment starts with a hash mark (
#), and the interpreter ignores everything from the hash mark to the end of the line. - There's no multiline comment syntax , so you either start each line with
#or use a bare string that the compiler drops from the bytecode. - A triple-quoted string right after a
def, aclass, or at the top of a module becomes a docstring , which is documentation rather than a comment. - PEP 8 recommends keeping comments to 72 characters per line, and editors like VS Code and PyCharm toggle comments with Ctrl + / .
- Good comments explain why the code does something, while comments that repeat the code, cover up smelly code, or insult a teammate are worth avoiding.
In this tutorial, you'll cover some of the basics of writing comments in Python. You'll learn how to write comments that are clean and concise, and when you might not need to write any comments at all. You'll also learn why commenting matters, which commenting habits to avoid, and how to practice writing cleaner comments.
Free Bonus: 5 Thoughts On Python Mastery , a free course for Python developers that shows you the roadmap and the mindset you'll need to take your Python skills to the next level.
Take the Quiz: Test your knowledge with our interactive "Writing Comments in Python (Guide)" quiz. You'll receive a score upon completion to help you track your learning progress:
Interactive Quiz
Writing Comments in Python (Guide)Test your understanding of how to write clean, useful Python comments, from basic syntax and docstrings to the comment types you should avoid.
Get Your Code: Click here to download the free sample code you'll use to practice writing clean, helpful comments and docstrings in Python.
Python for Beginners Live Course: Want feedback on how readable your own code is? In Real Python's Python for Beginners live course, you write real programs with an instructor and get live feedback on your code as you go. Join the waitlist to hear about the next dates.
Why Commenting Your Code Is So Important
Comments are an integral part of any program. They can come in the form of module-level docstrings , or even inline explanations that help shed light on a complex function.
Before diving into the different types of comments, take a closer look at why commenting your code is so important.
Consider the following two scenarios in which a programmer decided not to comment their code.
Remove adsWhen Reading Your Own Code
Client A wants a last-minute deployment for their web service. You're already on a tight deadline, so you decide to just make it work. All that "extra" stuff -- documentation , proper commenting, and so forth -- you'll add that later.
The deadline comes, and you deploy the service, right on time. Whew!
You make a mental note to go back and update the comments, but before you can put it on your to-do list, your boss comes over with a new project that you need to get started on immediately. Within a few days, you've completely forgotten that you were supposed to go back and properly comment the code you wrote for Client A.
Fast forward six months, and Client A needs a patch built for that same service to comply with some new requirements. It's your job to maintain it, since you were the one who built it in the first place. You open up your text editor and...
What did you even write?!
You spend hours parsing through your old code, but you're completely lost in the mess. You were in such a rush at the time that you didn't name your variables properly or even set your functions up in the proper control flow . Worst of all, you don't have any comments in the script to tell you what's what!
Developers forget what their own code does all the time, especially if it was written a long time ago or under a lot of pressure. When a deadline is fast approaching, and hours in front of the computer have led to bloodshot eyes and cramped hands, that pressure can be reflected in the form of code that is messier than usual.
Once the project is submitted, many developers are too tired to go back and comment their code. When it's time to revisit it later down the line, they can spend hours trying to parse through what they wrote.
Writing comments as you go is a great way to prevent the above scenario from happening. Be nice to Future You!
When Others Are Reading Your Code
Imagine you're the only developer working on a small Django project . You understand your own code pretty well, so you don't tend to use comments or any other sort of documentation, and you like it that way. Comments take time to write and maintain, and you just don't see the point.
The only problem is, by the end of the year your "small Django project" has turned into a "20,000 lines of code" project, and your supervisor is bringing on additional developers to help maintain it.
The new devs work hard to quickly get up to speed, but within the first few days of working together, you've realized that they're having some trouble. You used some quirky variable names and wrote with super terse syntax. The new hires spend a lot of time stepping through your code line by line, trying to figure out how it all works. It takes a few days before they can even help you maintain it!
Using comments throughout your code can help other developers in situations like this one. Comments help other devs skim through your code and gain an understanding of how it all works very quickly. You can help ensure a smooth transition by choosing to comment your code from the outset of a project.
Now that you understand why it's so important to comment your code, it's time to go over some basics so you know how to do it properly. You'll start with the syntax and PEP 8's formatting rules, then look at how to spread a comment over several lines, and finish with a few editor shortcuts that save you some typing.
Python Commenting Basics
Comments are for developers. They describe parts of the code where necessary to facilitate the understanding of programmers, including yourself.
To write a comment in Python, put a hash mark (#) before your desired comment:
# This is a comment
Python ignores everything after the hash mark and up to the end of the line. You can insert them anywhere in your code, even inline with other code:
Language: Pythonprint("This will run.") # This won't run
When you run the above code, you will only see the output This will run. Everything else is ignored.
Comments should be short, sweet, and to the point. While PEP 8 advises keeping code at 79 characters or fewer per line , it suggests a max of 72 characters for inline comments and docstrings. If your comment is approaching or exceeding that length, then you'll want to spread it out over multiple lines.
PEP 8 also has a few formatting rules for comments . Start a comment with a single hash mark followed by one space, write it as a complete sentence that starts with a capital letter, and separate an inline comment from the code by at least two spaces:
Language: Python# Block comments sit on their own line above the code they describe
total = price * quantity # Inline comments follow two spaces
Linters that check PEP 8 compliance will flag a missing space after the hash mark, so following these rules from the start saves you a round of cleanup later. You can learn more about the style guide in How to Write Beautiful Python Code With PEP 8 .
Remove adsUnfortunately, Python doesn't have a way to write multiline comments as you can in languages such as C , Java , and Go:
Language: Python# So you can't
just do this
in python
In the above example, the first line will be ignored by the program, but the other lines will raise a Syntax Error .
In contrast, a language like Java will allow you to spread a comment out over multiple lines:
Language: Java/* You can easily
write multiline
comments in Java */
Everything between /* and */ is ignored by the program.
While Python doesn't have native multiline commenting functionality, you can create multiline comments in Python. There are two ways to do so.
The first way is to press Enter after each line, add a new hash mark, and continue your comment from there:
Language: Pythondef multiline_example():
# This is a pretty good example
# of how you can spread comments
# over multiple lines in Python
print("Each comment line above is ignored")
Each line that starts with a hash mark will be ignored by the program. Note that the comments alone don't make a valid function body, though. If you leave out the print() call, then you get an IndentationError , because comments aren't statements.
Another thing you can do is use multiline strings by wrapping your comment inside a set of triple quotes:
Language: Python"""
If I really hate pressing `enter` and
typing all those hash marks, I could
just do this instead
"""
This is like multiline comments in Java, where everything enclosed in the triple quotes will function as a comment.
While this gives you the multiline functionality, this isn't technically a comment. It's a string literal that's not assigned to any variable, so it's not called or referenced by your program. Still, since the compiler drops it and it won't appear in the bytecode , it can effectively act as a comment at runtime . You can check this yourself by looking at the constants that Python stores for a function:
Language: Python>>> def greet():
... print("Hello")
... """
... This string isn't assigned to anything,
... so it can act like a comment
... """
... print("Goodbye")
...
>>> greet.__code__.co_consts
('Hello', 'Goodbye')
The two strings that print() uses are there, but the triple-quoted text in between is gone. On Python 3.14 and earlier, you'll also see a None in the tuple, but the string is never part of it. You can take a look at this article for a closer look at the bytecode.
However, be careful where you place these multiline "comments." Depending on where they sit in your program, they could turn into docstrings, which are pieces of documentation that are associated with a function or method . If you slip one of these bad boys right after a function definition, then what you intended to be a comment will become associated with that object.
Because of this ambiguity, you may want to reserve triple quotes for docstrings and skip string comments entirely. When in doubt, just put a hash mark on each subsequent line. If you're interested in learning more about docstrings and how to associate them with modules , classes, and the like, check out our tutorial on Documenting Python Code .
Python Commenting Shortcuts
It can be tedious to type out all those hash marks every time you need to add a comment. So what can you do to speed things up a bit? Here are a few tricks to help you out when commenting. The animations below were recorded in Sublime Text , but you'll find the same features in VS Code , PyCharm , and most other editors.
One of the first things you can do is use multiple cursors. That's exactly what it sounds like: placing more than one cursor on your screen to accomplish a task. In Sublime Text, hold down Ctrl on Windows and Linux or Cmd on macOS while you left-click, and you should see the blinking lines on your screen:
This is most effective when you need to comment the same thing in several places.
What if you've got a long stretch of text that needs to be commented out? Say you don't want a defined function to run while you check for a bug. Clicking each and every line to comment it out could take a lot of time! In these cases, you'll want to toggle comments instead. Select the desired code and press Ctrl + / on Windows and Linux, or Cmd + / on macOS:
All the highlighted text will be prepended with a hash mark and ignored by the program.
If your comments are getting too unwieldy, or the comments in a script you're reading are really long, then your text editor may give you the option to collapse them using the small down arrow on the left-hand side:
Click the arrow to hide the comments. This works best with long comments spread out over multiple lines, or docstrings that take up most of the start of a program.
The default key bindings differ a bit from editor to editor. Here's how the three tricks map onto the most popular editors on Windows and Linux:
| Trick | VS Code | PyCharm | Sublime Text |
|---|---|---|---|
| Add a cursor | Alt + Click | Alt + Click | Ctrl + Click |
| Toggle comments | Ctrl + / | Ctrl + / | Ctrl + / |
| Collapse comments | Arrow in the gutter | Arrow in the gutter | Arrow in the gutter |
On macOS, the defaults use Cmd and Option instead of Ctrl and Alt . If a shortcut doesn't do anything in your editor, then look it up in the editor's keymap settings, where you can also change it to whatever you like.
Combining these tips will make commenting your code quick and painless!
Remove adsPython Commenting Best Practices
While it's good to know how to write comments in Python, it's just as vital to make sure that your comments are readable and clear.
Take a look at these tips to help you write comments that really support your code. First up are the comments that help you, and then the ones that help everyone else who reads your code.
When Writing Code for Yourself
You'll thank yourself later if you comment your own code properly. Even if no one else will ever see it, you'll see it, and that's enough reason to make it right. You're a developer after all, so your code should be easy for you to understand as well.
One extremely useful way to use comments for yourself is as an outline for your code. If you're not sure how your program is going to turn out, then you can use comments as a way to keep track of what's left to do, or even as a way of tracking the high-level flow of your program. For instance, use comments to outline a function in pseudo-code :
Language: Pythonfrom collections import defaultdict
def get_top_cities(prices):
top_cities = defaultdict(int)
# For each price range
# Get city searches in that price
# Count num times city was searched
# Take top 3 cities & add to dict
return dict(top_cities)
These comments plan out get_top_cities(). Once you know exactly what you want your function to do, you can work on translating that to code.
Using comments like this can help keep everything straight in your head. As you walk through your program, you'll know what's left to do to have a fully functional script. After translating the comments to code, remember to remove any comments that have become redundant so that your code stays crisp and clean.
You can also use comments as part of the debugging process. Comment out the old code and see how that affects your output. If you agree with the change, then don't leave the code commented out in your program, as it decreases readability. Delete it and use version control if you need to bring it back.
Finally, use comments to define tricky parts of your own code. If you put a project down and come back to it months or years later, you'll spend a lot of time trying to get reacquainted with what you wrote. In case you forget what your own code does, do Future You a favor and mark it down so that it will be easier to get back up to speed later on.
When Writing Code for Others
People like to skim and jump back and forth through text, and reading code is no different. The only time you'll probably read through code line by line is when it isn't working and you have to figure out what's going on.
In most other cases, you'll take a quick glance at variables and function definitions to get the gist. Having comments to explain what's happening in plain English can really assist a developer in this position.
Be nice to your fellow devs and use comments to help them skim through your code. Inline comments should be used sparingly to clear up bits of code that aren't obvious on their own. Of course, your first priority should be to make your code stand on its own, but inline comments can be useful in this regard.
If you have a complicated method or function whose name isn't easily understandable, you may want to include a short comment after the def line to shed some light:
def complicated_function(s):
# This function does something complicated
...
This can help other devs who are skimming your code get a feel for what the function does. The ellipsis (...) stands in for the function's body here, so the example is valid Python even without any real code in it.
For any public functions, you'll want to include an associated docstring , whether it's complicated or not:
Language: Pythonimport math
def sparsity_ratio(values: list[float]) -> float:
"""Return the fraction of values that are zero or NaN."""
sparse = [value for value in values if value == 0 or math.isnan(value)]
return len(sparse) / len(values)
This string will become the .__doc__ attribute of your function and will officially be associated with that specific function. You can confirm this in the REPL:
>>> sparsity_ratio.__doc__
'Return the fraction of values that are zero or NaN.'
>>> sparsity_ratio([0, 1.5, float("nan"), 2])
0.5
The same text shows up when you call help(sparsity_ratio), which is how other developers will usually read it.
The PEP 257 docstring guidelines will help you to structure your docstring. These are a set of conventions that developers generally use when structuring docstrings. For example, a one-line docstring should describe what the function does as a command, which is why the docstring above starts with Return rather than Returns or This function returns .
The PEP 257 guidelines have conventions for multiline docstrings as well. A module-level docstring is one example. It appears right at the top of a file and includes a high-level overview of the entire script and what it's supposed to do:
Language: Python"""A module-level docstring
Docstrings do appear in the bytecode, so you can access this through
the ``__doc__`` attribute. This is also what you'll see if you call
help() on a module or any other Python object.
"""
A module-level docstring like this one will contain any pertinent or need-to-know information for the developer reading it. When writing one, it's recommended to list out all classes, exceptions, and functions as well as a one-line summary for each. To go deeper into the different docstring formats and the tools that read them, check out How to Write Docstrings in Python .
Remove adsPython Commenting Worst Practices
Just as there are standards for writing Python comments, there are a few types of comments that don't lead to Pythonic code . Here are just a few. You'll probably recognize each of them from a codebase you've worked on, so it helps to know what to look for.
Your comments should be D.R.Y. The acronym stands for the programming maxim "Don't Repeat Yourself." This means that your code should have little to no redundancy. You don't need to comment a piece of code that sufficiently explains itself, like this one:
Language: Pythonreturn a # Returns a
It's clear that a is returned, so there's no need to explicitly state this in a comment. This makes comments W.E.T., meaning you "wrote everything twice." (Or, for the more cynical out there, "wasted everyone's time.")
W.E.T. comments can be a simple mistake, especially if you used comments to plan out your code before writing it. But once you've got the code running well, be sure to go back and remove comments that have become unnecessary.
Comments can be a sign of "code smell ," which is anything that indicates there might be a deeper problem with your code. Code smells try to mask the underlying issues of a program, and comments are one way to try and hide those problems. Comments should support your code, not try to explain it away. If your code is poorly written, no amount of commenting is going to fix it.
Take this example:
Language: Python# A dictionary of families who live in each city
mydict = {
"Midtown": ["Powell", "Brantley", "Young"],
"Norcross": ["Montgomery"],
"Ackworth": []
}
def a(dict):
# For each city
for p in dict:
# If there are no families in the city
if not mydict[p]:
# Say that there are no families
print("None.")
This code is quite unruly. There's a comment before every line explaining what the code does. This script could have been made simpler by assigning obvious names to variables, functions, and collections, like so:
Language: Pythonfamilies_by_city = {
"Midtown": ["Powell", "Brantley", "Young"],
"Norcross": ["Montgomery"],
"Ackworth": [],
}
def no_families(cities):
for city in cities:
if not families_by_city[city]:
print(f"No families in {city}.")
By using descriptive names, you can remove all the unnecessary comments and shorten the code as well!
Your comments should rarely be longer than the code they support. If you're spending too much time explaining what you did, then you need to go back and refactor to make your code more clear and concise.
This is something that's likely to come up when working on a development team. When several people are all working on the same code, others are going to be going in and reviewing what you've written and making changes. From time to time, you might come across someone who dared to write a comment like this one:
Language: Python# Put this here to fix Ryan's stupid-a** mistake
Honestly, it's just a good idea to not do this. It's not okay if it's your friend's code, and you're sure they won't be offended by it. You never know what might get shipped to production, and how is it going to look if you'd accidentally left that comment in there, and a client discovered it down the road? You're a professional, and including vulgar words in your comments is not the way to show that.
How to Practice Commenting
The best way to start writing more Pythonic comments is just to do it!
Start writing comments for yourself in your own code. Make it a point to include simple comments from now on where necessary. Add some clarity to complex functions, and put a docstring at the top of all your scripts.
Another good way to practice is to go back and review old code that you've written. See where anything might not make sense, and clean up the code. If it still needs some extra support, add a quick comment to help clarify the code's purpose.
This is an especially good idea if your code is up on GitHub and people are forking your repo. Help them get started by guiding them through what you've already done.
You can also give back to the community by commenting other people's code. If you've downloaded something from GitHub and had trouble sifting through it, add comments as you come to understand what each piece of code does.
"Sign" your comment with your initials and the date, and then submit your changes as a pull request. If your changes are merged, you could be helping dozens if not hundreds of developers like yourself get a leg up on their next project.
Remove adsConclusion
Learning to comment well is a valuable tool. Not only will you learn how to write more clearly and concisely in general, but you'll no doubt gain a deeper understanding of Python as well.
Knowing how to write comments in Python can make life easier for all developers, including yourself! They can help other devs get up to speed on what your code does, and help you get re-acquainted with old code of your own.
By noticing when you're using comments to try and support poorly written code, you'll be able to go back and modify your code to be more robust. Commenting previously written code, whether your own or another developer's, is a great way to practice writing clean comments in Python.
As you learn more about documenting your code, you can consider moving on to the next level of documentation. Check out our tutorial on Documenting Python Code to take the next step.
Get Your Code: Click here to download the free sample code you'll use to practice writing clean, helpful comments and docstrings in Python.
Frequently Asked Questions
Now that you have some experience with commenting your code, you can use the questions and answers below to check your understanding and recap what you've learned.
These FAQs are related to the most important concepts you've covered in this tutorial. Click the Show/Hide toggle beside each question to reveal the answer.
You start a comment with a hash mark (#), and the interpreter ignores everything from the hash mark to the end of the line. A comment can sit on its own line or follow a statement on the same line, as long as you leave at least two spaces before the hash mark.
There's no dedicated multiline comment syntax, so the usual approach is to start each line with a hash mark. You can also use a bare triple-quoted string, which the compiler drops from the bytecode, but it turns into a docstring if it's the first statement in a module, class, or function.
A comment is text that the interpreter throws away, so it only exists for the people reading the source. A docstring is a string that Python keeps in the .__doc__ attribute of a module, class, or function, which is what help() and documentation tools display.
PEP 8 recommends a maximum of 72 characters per line for comments and docstrings, while code lines can go up to 79 characters. If a comment needs more room, then spread it over several lines, each starting with its own hash mark.
Most editors let you select the lines and toggle comments with a shortcut, which is Ctrl+/ in VS Code, PyCharm, and Sublime Text on Windows and Linux, and Cmd+/ on macOS. Each selected line gets a hash mark in front of it, and pressing the shortcut again removes the hash marks.
Take the Quiz: Test your knowledge with our interactive "Writing Comments in Python (Guide)" quiz. You'll receive a score upon completion to help you track your learning progress:
Interactive Quiz
Writing Comments in Python (Guide)Test your understanding of how to write clean, useful Python comments, from basic syntax and docstrings to the comment types you should avoid.
Liked it (thumbs up) Disliked it (thumbs down)Recommended Course
Writing Comments in Python (29m)
🐍 Python Tricks 💌
Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.
About Jaya Zhané
Jaya is an avid Pythonista and writes for Real Python. She's a Master's student at Georgia Tech and is interested in data science, AI, machine learning and natural language processing.
» More about JayaEach tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:
Aldren
Brad
Joanna
Martin
Master Real-World Python Skills With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
Master Real-World Python Skills
With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
Keep Learning
Related Topics: basics best-practices
Related Learning Paths:
Related Courses:
Related Tutorials: