Python Multiline Comments

Learn how to write multiline comments in Python using multiple # comments. Understand why triple-quoted strings are not true comments, how docstrings differ from comments, and how to comment multiple lines in VS Code, PyCharm, and other editors.

Published

Updated

Read time 5 min read

Reviewed byDeepak Prasad

Python Multiline Comments

If you want readable “paragraph” explanations in code, you still use the same # mechanism on every line. This page answers whether Python has a dedicated multiline comment, shows the recommended pattern, contrasts triple quotes and docstrings, lists editor shortcuts, and ends with pitfalls and a small lookup table.

Tested on: Python 3.13.3; kernel 6.14.0-37-generic; Ubuntu 25.04.


Does Python support multiline comments?

python
# This is a multiline comment.
# Each line starts with #.
# Python does not have a separate multiline comment syntax.
Output

No. There is no second syntax like /* ... */ in C, Java, or JavaScript—only # through end-of-line is a comment. To “comment a paragraph,” repeat # at the start of each line. Prefix # plus a space for readability (PEP 8), and align # with the code block you describe so the scope is obvious.

python
# Compute moving average over window k.
# Caller must pass k >= 1; empty series returns [].
def moving_average(values, k):
    ...
Output

# lines add no runtime objects, work in tight layouts, and match what linters and reviewers expect. In the language itself there is only this one style of comment; what changes is how you produce many # lines—by typing them or by letting the editor insert them for a selection (see below). That is convenience, not a second comment syntax.


Triple quotes and docstrings

Triple quotes always define a string literal, not a lexer-level comment. A module-level """...""" before other code still creates a string object that is then discarded if unused—it is easy to misread and may annoy style checkers.

python
"""
This looks like a comment block but it is a string value.
Python creates the object, then throws it away if it is not assigned.
"""
x = 1
Output

If the first statement in a module, class, or function is a string literal, Python stores it as __doc__ (a docstring). That is for API documentation and help(), not for ordinary narration in the middle of logic.

python
class Car:
    """This class contains all the info about car."""

print(Car.__doc__.strip())
Output

Running this prints one line: the sentence inside the triple quotes. Avoid triple-quoted “comments” mid-function, as trailing notes, or to disable code; use # lines or editor folding, and use line-comment toggles to turn real code off temporarily.

A comment starts with # and runs to the newline; the interpreter never executes that text. A docstring is a string (usually triple-quoted) that appears as the first statement in a module, class, or function and is stored in __doc__.

Aspect Comment (#) Docstring (""" / ''')
Purpose Explain code to readers Document public API of module/class/function
Syntax # to end of line String as first statement in suite
Runtime Stripped by lexer Stored in __doc__ (first string only)
Mid-function notes Use # on each line Do not use bare triple strings as “comments”

For broader learning context, see beginner tips for learning Python. When a comment sits beside a long expression, line continuation rules explain where you may break lines; for multi-line string literals (not comments), see multi-line strings.


Editors, block style, and inline comments

For multiline remarks, select the lines you want, then run the editor’s line-comment action. It prefixes # on each selected line; run the same command again to remove those # characters. Typical bindings:

  • VS Code: Ctrl+/ on Windows and Linux, Cmd+/ on macOS (Toggle Line Comment).
  • PyCharm and other JetBrains IDEs: Ctrl+/ on Windows and Linux, Cmd+/ on macOS (Comment with Line Comment).
  • JupyterLab or the classic notebook: with the cursor in a cell, select lines and use Ctrl+/ or Cmd+/ in the cell editor; behavior matches most code editors, but keymaps can differ if you customized them.
  • Other editors: search the command palette for “line comment” or “toggle comment” and bind the action that inserts # per line for Python—avoid language modes that insert /* */, which are not valid in Python.

A block comment in Python style is several consecutive # lines placed before the code they describe, at the same indent as that code:

python
# Validate bounds before indexing.
    # Raises ValueError if out of range.
    if not 0 <= i < len(items):
        raise ValueError("index out of range")
Output

An inline comment sits after a statement on the same line; everything after # is the comment. Keep these short; put longer reasoning on full # lines above.

python
limit = 100  # default page size
Output

Common mistakes and quick reference

  • Treating """...""" in the middle of a function as “free” commentary—it still builds a string.
  • Mis-indenting a triple-quoted string so it does not attach where you think; you can get syntax errors or accidental string concatenation.
  • Forgetting that only the first string in a function becomes the docstring; later strings are ordinary expressions.
  • Using a VS Code “block comment” action meant for other languages that inserts /* */—that is not valid Python.
Need What to do
Multiline explanation Several # lines
Quick disable code Editor toggles # on each selected line
API documentation Docstring as first statement in module/class/def
Not a comment Triple-quoted string not in docstring position

Summary

Python has no /* */-style multiline comment: use # on each line, optionally via your editor’s toggle. Triple quotes always define strings; when they are the first statement of a module, class, or function they become docstrings in __doc__. Reserve docstrings for real API documentation and use # for ordinary multiline remarks so behavior stays clear to humans and tools.


References


Frequently Asked Questions

1. Does Python have a multiline comment operator like /* */?

No. Only single-line comments with # are true comments; the usual multiline style is one # per line, optionally created by your editor for a whole selection.

2. Can I use triple quotes for multiline comments in Python?

Triple quotes define string literals; if they are not the first statement of a module, class, or function they still build a string object that is discarded, which is misleading and can confuse tools—use # lines instead.

3. What is the difference between a comment and a docstring?

Comments use # and are removed by the lexer; docstrings are normal strings placed as the first statement in a module, class, or function and are stored in doc for introspection and documentation tools.
Bashir Alam

Data Analyst and Machine Learning Engineer

Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and …