How do I create multiline comments in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python does not have a dedicated multiline-comment syntax like /* ... */ in some other languages. In practice, Python developers either use repeated # comment lines for real comments or triple-quoted strings for docstrings and, in some limited cases, block text that is being used like a comment.
Core Sections
Real comments in Python use #
The official comment syntax in Python is the hash character. If you want to comment several lines, the normal approach is simply to repeat # on each line.
This is the safest answer because the interpreter always treats those lines as comments.
Why triple-quoted strings are often misunderstood
Many people say Python uses triple quotes for multiline comments. That is not literally true. Triple quotes create string literals.
If the string is not assigned to anything and appears in a place where it is not used, it often has no practical effect beyond being created and ignored. That is why it behaves like a comment in many simple examples. But it is still a string, not comment syntax.
Use triple quotes for docstrings, not general commenting
Triple-quoted strings are officially important for docstrings, which document modules, classes, and functions.
Docstrings are not just decorative text. Tools, editors, and the runtime can inspect them. That makes them the right use of triple-quoted strings when you are documenting public code.
When a triple-quoted block is acceptable
There are cases where developers temporarily use an unassigned triple-quoted string to disable a code block during experimentation. It can work, but it should be treated as a short-lived convenience rather than a style recommendation.
The reason this is not ideal is that the block is still syntactically a string literal. In some contexts, that can be misleading to readers, and in others it can create unintended behavior if it ends up as a function or module docstring.
Prefer explicit comments for maintainable code
If your goal is explanation, use real comments. If your goal is documentation, use docstrings. If your goal is to temporarily disable code, use editor tools, version control, or short-lived # comments.
That keeps the meaning of each construct clear:
- '
#for comments' - triple quotes for strings and docstrings
- version control for tracking disabled code rather than leaving large commented-out blocks around forever
This distinction matters because readable code depends on readers knowing whether a block is explanatory text, runtime documentation, or just old code that should probably be deleted.
Common Pitfalls
- Calling triple-quoted strings "multiline comments" hides the fact that Python is actually parsing a string literal.
- Leaving large blocks of dead code in triple-quoted strings makes the file harder to maintain and review.
- Accidentally creating a docstring when you meant to leave a temporary note can change introspection behavior.
- Using triple-quoted strings instead of repeated
#lines for ordinary comments confuses readers about intent. - Treating commented-out code as long-term storage is a version-control problem, not a commenting strategy.
Summary
- Python’s real comment syntax is
#. - For multiline comments, the normal practice is repeated
#lines. - Triple-quoted text is a string literal, not a special multiline-comment syntax.
- Triple quotes are the correct tool for docstrings on modules, classes, and functions.
- Use comments, docstrings, and version control for their distinct purposes instead of relying on one construct for all three.
Related reading
- How do I create multiline comments in Python?
- How do I create test and train samples from one dataframe with pandas?
- How do I declare an array in Python?
- How do I declare custom exceptions in modern Python?
- How do I define a function with optional arguments?
- How do I detect the Python version at runtime?
- How do I determine if my python shell is executing in 32bit or 64bit?
- How do I determine the size of an object in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.