4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

How to comment multiple lines in Python

By 4Geeks

Comment out several lines at once with # and your editor's shortcut, use triple quotes only for docstrings and avoid the common errors.

5 min readLast updated
Comment multiple lines in Python icon

How do you comment out multiple lines in Python?

Short answer: Python has no block comment syntax, so you put # at the start of each line. You don't have to type them one by one: select the lines in your editor and press Ctrl + / (Cmd + / on macOS). Press it again to uncomment.

python
# print("Step 1")
# print("Step 2")
print("Step 3")   # only this line runs

Triple quotes (""" or ''') also hide text, but they create a string, not a comment. Use them for docstrings, not to switch off code.

Put # on each line with Ctrl + / and Python skips those lines when it runs the file.

Shortcuts to comment multiple lines in each editor

Select the lines first, then press:

EditorComment / uncomment
VS CodeCtrl + / (macOS: Cmd + /)
VS Code, alternativeCtrl + K, Ctrl + C to comment and Ctrl + K, Ctrl + U to uncomment
PyCharmCtrl + / (macOS: Cmd + /)
Jupyter Notebook and JupyterLabCtrl + / (macOS: Cmd + /)
SpyderCtrl + 1 (macOS: Cmd + 1)
IDLEAlt + 3 to comment and Alt + 4 to uncomment
Sublime TextCtrl + / (macOS: Cmd + /)
VimCtrl + V, select the lines, Shift + I, type #, Esc

All of them add # to each line. None of them insert triple quotes.

Comment multiple lines with

The # symbol turns the rest of the line into a comment. For several lines, start each one with #:

python
# Calculate the total price of the order.
# Prices are in dollars and already include tax.
total = 0
for price in [10, 20, 5]:
    total += price
print(total)  # 35

This is what PEP 8, the Python style guide, recommends for block comments: each line starts with # and a single space, with the same indentation as the code it describes.

You can also add a comment at the end of a line of code. Leave at least two spaces before the #:

python
discount = 0.1  # 10% for new customers
Career · AI Engineering

You already write clean Python, now build with AI

From zero to junior AI Engineer: Python, data, AI agents and RAG with mentors and real projects.

Triple quotes: docstrings, not comments

A string between triple quotes can span several lines, so some people use it as a multi-line comment:

python
"""
print("Step 1")
print("Step 2")
"""
print("Step 3")  # Step 3

It works because Python creates the string and then throws it away. But it is still code, not a comment, and that has consequences:

  • It must follow the indentation of the block where it is.
  • Backslashes are read as escape sequences, so a Windows path like C:\Users inside it raises a SyntaxError.
  • If the code you hide already contains triple quotes, the string ends early.

Where triple quotes really belong is in docstrings: the first string inside a function, class or module, which documents it.

python
def total(prices):
    """Return the sum of a list of prices.
 
    prices: a list of numbers in dollars.
    """
    return sum(prices)
 
print(total.__doc__)
help(total)

Python saves the docstring in __doc__, and help() and your editor show it when you hover over the function.

# comments vs triple quotes

# commentsTriple quotes
What it isA real comment, Python ignores itA string that Python creates and discards
Best forExplaining code and switching code offDocstrings for functions, classes and modules
IndentationAnyMust match the block
Editor shortcutCtrl + /None
Shown by help()NoYes, when it is a docstring

Switch off a large block of code

When you need to disable a long piece of code for a while, you have three options:

  1. Select it and press Ctrl + /. This is the cleanest one.
  2. Wrap it in if False: and indent it. Python skips the block, and your editor still checks its syntax.
  3. Delete it. If you use Git, the old version stays in the history, so you don't need to keep dead code commented out.
python
if False:
    send_email(user)
    log_event("email sent")

Use AI to write and clean up comments

An assistant like ChatGPT, Claude or GitHub Copilot can help you write useful comments and docstrings. These three prompts are ready to copy.

1. Write a docstring

Write a docstring for this Python function following PEP 257.
Explain the parameters, the return value and any errors it can raise: [paste the function]

2. Clean up comments

Review the comments in this Python code. Remove the ones that only repeat what the code says,
and rewrite the rest so they explain why, not what: [paste the code]

3. Understand commented-out code

This Python file has blocks of code commented out with # and triple quotes: [paste the code].
Tell me which blocks are safe to delete and which ones might still be needed.

Read every suggestion before you paste it into your project.

Learn with a mentor

From your first comments to AI applications

In the 4Geeks AI Engineering career program you start with no experience and finish with AI agents and RAG systems in your portfolio.

Common problems when commenting in Python

What you seeWhy it happensFix
IndentationError: expected an indented block after function definitionA triple-quoted string inside a function is not indentedIndent it like the code, or use #
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytesA triple-quoted "comment" contains a backslash, such as C:\UsersUse # comments, or an r"""...""" string
The commented code still runsYou commented only some of the linesSelect the whole block and press Ctrl + /
Ctrl + / does nothingThe shortcut is different in your editor or keyboard layoutUse the table above, or the menu, such as Edit > Toggle Line Comment in VS Code
print("#1") prints #1A # inside a string is text, not a commentIt is correct; comments only start outside strings
/* ... */ raises a SyntaxErrorThat is JavaScript or C syntaxUse # on each line
Free download

Commenting in Python in a 2-page cheat sheet

The # rule, shortcuts for VS Code, PyCharm, Jupyter, Spyder, IDLE and Vim, docstrings vs comments, common errors and prompts for ChatGPT, Claude or Copilot. Free, in PDF.

How to comment multiple lines in Python cheat sheet

2 pages · PDF · 4Geeks

  • The comment shortcut for every popular editor
  • When to use # and when to use docstrings
  • Common errors and prompts to solve them with AI

Get it free

Your PDF right away

Frequently asked questions about comments in Python

Want to go from writing your first Python scripts to building AI apps, with real projects and mentors?

See the AI Engineering program