How to Comment Out Multiple Lines in Python

What you’ll build or solve

You’ll disable a block of Python code without deleting it, so you can test changes faster.

When this approach works best

This approach works well when you:

  • Debug a script and want to skip a block without removing it.
  • Isolate a bug by turning off one loop, function call, or branch.
  • Temporarily disable slow or side-effect code, such as API calls or file writes, during testing.

Skip this approach when:

  • You’re cleaning up old code for real. Delete it and rely on version control if you need it back.

Prerequisites

  • Any Python version
  • A code editor such as VS Code, PyCharm, or any editor that can edit Python

Step-by-step instructions

1) Comment out lines with # (standard approach)

Python supports single-line comments using #. For multiple lines, the standard approach is to add # to each line.

Block you want to disable:

total=0
forninrange(1,6):
total+=n
print(total)

Comment it out:

# total = 0
# for n in range(1, 6):
#     total += n
# print(total)

What to look for

Keep indentation after # on lines that were inside a block. The code stays readable and is easy to uncomment later.


2) Use your editor’s “toggle line comment” shortcut

Most editors can comment or uncomment a selected block in one action.

Common shortcuts:

  • Windows/Linux in many editors: Ctrl + /
  • macOS in many editors: Cmd + /

If your editor uses a different shortcut, open the command palette and search for “Toggle Line Comment.”

What to look for

Make sure your selection includes every line you want to comment. If you miss a line inside a block, you may create syntax errors.


3) Verify the file still parses

After commenting out a block, run the script or run a quick syntax check.

Run the file:

python3 your_script.py

Or compile it without executing:

python3-m py_compile your_script.py

What to look for

An IndentationError usually means you commented out the body of a block but left the header behind, such as if, for, or def.


Examples you can copy

Example 1: Temporarily disable a function call

defsend_email(user_id:int) ->None:
print(f"Sending email to{user_id}")

user_id=42

# send_email(user_id)
print("Finished")

Example 2: Comment out a loop while keeping the rest of the script

items= ["apple","banana","cherry"]

# for item in items:
#     print(item.upper())

print("Loop disabled for now")

Example 3: Comment out part of a list cleanly

ports= [
3000,
4000,
# 5000,
8000,
]

This works well inside parentheses, brackets, and braces.


Common mistakes and how to fix them

Mistake 1: Using triple quotes as “multi-line comments”

You might try:

"""
for i in range(3):
    print(i)
"""
print("done")

Why it breaks

Triple-quoted text creates a string literal, not a real comment. It can also cause confusing issues if the disabled code contains quotes.

Fix

Use # comments instead:

# for i in range(3):
#     print(i)

Mistake 2: Leaving a block header active but removing its body

You might write:

ifcondition:
#     do_work()
print("done")

Why it breaks

Python expects an indented block after if, for, while, def, class, try, and similar statements.

Fix

Comment out the whole block:

# if condition:
#     do_work()
# print("done")

Or keep the structure valid with a placeholder:

ifcondition:
pass

print("done")

Mistake 3: Wrapping code in if False: and forgetting it

You might write:

ifFalse:
run_important_step()

Why it breaks

This is a debugging trick, not a real comment. It can confuse reviewers, linters, and your future self.

Fix

Use real comments:

# run_important_step()

Or delete the code if you no longer need it.


Troubleshooting

If you see: IndentationError: expected an indented block

Comment out the whole block, or add pass as the body.


If you see: SyntaxError after commenting out a few lines

Run:

python3-m py_compile your_script.py

Then comment out a complete statement instead of only part of it.


If your editor comments only some lines

Reselect the full block, then toggle line comment again.


If commenting inside parentheses breaks formatting

Put the # at the start of the line you want to disable, as shown in the list example.


Quick recap

  • Use # on each line to comment out multiple lines.
  • Use your editor shortcut to do it quickly.
  • After changes, run the script or python3 -m py_compile to confirm it still parses.
  • Avoid triple quotes and if False: as “comments” for disabling code.