How to Comment Out Multiple Lines in Python

When I work on a small reporting script, I often need to temporarily disable a few lines before testing a new calculation. Maybe the script reads a CSV file, formats rows, and writes a final report. Commenting out the right block lets me isolate the problem without deleting useful code.

Python keeps comments simple, but it does not have a dedicated block-comment symbol like some other languages. You need to use the right approach depending on whether you want to add notes, pause code during testing, or write documentation.

Here, you’ll learn the practical ways to comment out multiple lines in Python, when to use each one, and the mistakes to avoid.

How to Comment Out Multiple Lines in Python with #

The standard and safest way to comment out multiple lines in Python uses the hash symbol (#) at the beginning of every line.

Python ignores everything after # on that line. To comment out a block, add # before each line in the block.

report_name = "weekly_sales"

# total_sales = 0
# for amount in sales_amounts:
# total_sales += amount
#
# print(f"Total sales: {total_sales}")

print(f"Creating report: {report_name}")

You can refer to the screenshot below to see the output.

Comment Out Multiple Lines Python

In this example, Python runs only the final print() statement. It ignores the calculation block because every line starts with #.

This approach works in Python 3 and remains the best choice for production code, local automation scripts, data analysis notebooks, and server-side applications. It clearly tells other developers that you intentionally disabled those lines.

If you are still getting comfortable with indentation, read this guide on indenting multiple lines in Python. Indentation controls Python code blocks, so a missing space can cause an error even after you uncomment the code.

Comment out a section of a reporting script

Here is a realistic example from a file-processing script. The script reads a text file and saves a summary, but you may want to pause the save step while checking the output.

source_file = "sales_log.txt"
summary_file = "daily_summary.txt"

# with open(summary_file, "w", encoding="utf-8") as file:
# file.write("Daily sales summary\n")
# file.write("Report created successfully\n")

print(f"Summary ready for: {source_file}")

You can refer to the screenshot below to see the output.

Comment Out Multiple Lines in Python

The commented with open() block does not create or overwrite the summary file. That makes it useful when testing code that writes files, sends emails, updates databases, or calls an API.

Before you enable the block again, make sure your file path and file mode are correct. You may also find this guide on opening files in Python useful when building file automation scripts.

Pro Tip: I use # comments whenever I need to temporarily disable executable code. It is obvious, reversible, and does not create hidden strings in memory.

How to Comment Out Multiple Lines in Python with Editor Shortcuts

Typing # at the start of ten or twenty lines gets tedious. Most Python code editors let you select multiple lines and toggle comments with one keyboard shortcut.

The exact shortcut depends on your editor and operating system, but these are common options:

  • Windows and Linux: Select the lines and press Ctrl + /
  • macOS: Select the lines and press Cmd + /

Your editor adds # to each selected line. Press the shortcut again to remove the comment markers.

For example, select this code:

total_sales = 0
for amount in sales_amounts:
total_sales += amount

print(total_sales)

After toggling comments, it becomes:

# total_sales = 0
# for amount in sales_amounts:
# total_sales += amount
#
# print(total_sales)

This is still the same standard Python comment technique. The editor simply saves time by adding the # symbols for you.

Editor shortcuts help most when you debug a long function, test a branch in an automation script, or compare two versions of a calculation. If you need a refresher on creating reusable code blocks, see how to define a function in Python.

Can You Use Triple Quotes to Comment Out Multiple Lines?

You may see developers use triple quotes (""" or ''') around several lines of Python code.

"""
total_sales = 0

for amount in sales_amounts:
total_sales += amount

print(total_sales)
"""

This looks like a multi-line comment, but it is actually a multi-line string. Python creates a string object and then discards it if nothing uses it.

The code inside the triple quotes does not run, so it may appear to work during quick testing. However, I do not recommend it as your normal way to comment out multiple lines in Python.

Why triple quotes are not real comments

A real comment starts with #, and Python ignores it before the program runs. A triple-quoted value is still a string in Python source code.

This difference matters when your disabled code contains its own triple quotes.

"""
message = """Report created"""
print(message)
"""

Python cannot correctly determine where the outer string ends. You will get a syntax error instead of a cleanly disabled code block.

Triple quotes also have an important valid use: writing docstrings. A docstring is a string placed at the beginning of a module, class, or function to explain what it does.

def calculate_total(amounts):
"""Return the total from a list of sales amounts."""
return sum(amounts)

Here, the triple-quoted text documents the function. It does not act as a comment. Python tools and developers can read this documentation later.

For multi-line text that your program must actually use, see this guide on using triple quotes with f-strings for multiline strings.

Use if False Carefully

Another technique wraps code in an if False: block.

if False:
total_sales = 0

for amount in sales_amounts:
total_sales += amount

print(total_sales)

print("Report test completed")

You can refer to the screenshot below to see the output.

How to Comment Out Multiple Lines in Python

Python does not run the indented block because the condition is always False. This technique can help you keep indentation intact while you temporarily skip a large block.

However, this is not a true comment. Python still reads and parses the code inside the block. A syntax error inside it can still stop your script from starting.

For example, this still fails:

if False:
print("Starting report"

The closing parenthesis is missing, so Python raises a syntax error before it decides whether the condition is true or false.

Use if False: only for short-lived debugging experiments. Use # comments when you want to comment out code clearly and safely. If Python reports a confusing error during testing, this guide on invalid syntax errors in Python can help you track it down.

When Should You Comment Out Code?

Commenting out code helps during development, but leaving large commented blocks in a finished script creates clutter. I use it for temporary tests, not permanent code history.

For example, you might comment out a file-writing section while checking calculations:

def create_sales_summary(amounts):
total = sum(amounts)
average = total / len(amounts)

# print(f"Total sales: {total}")
# print(f"Average sale: {average:.2f}")

return total, average

The function still returns the values, but it does not print temporary debug output. This lets you test the function in another script without unnecessary console messages.

When you finish testing, either remove unused code or keep a short comment that explains an important decision. Version control is better for preserving old approaches than leaving dozens of disabled lines in a Python file.

Things to Keep in Mind

  • Use # for real comments: Prefix each line with # when you want Python to ignore code completely.
  • Avoid triple quotes for disabled code: Triple quotes create strings, not comments, and nested quotes can break your script.
  • Watch indentation: When you uncomment a block, restore its original indentation or Python may raise an error.
  • Do not hide secrets: Commented API keys, passwords, tokens, and database details remain visible in source files.
  • Remove temporary code: Clean up large commented sections after testing so future updates stay easy to understand.
  • Use editor shortcuts: Toggle comments on selected lines instead of manually adding or removing # one line at a time.

Frequently Asked Questions

How do I comment out multiple lines in Python?

Add # at the beginning of every line you want to disable. You can do this manually or select the lines in your editor and use its toggle-comment shortcut.

Does Python have block comments?

No, Python does not have a separate block-comment syntax. The standard method uses # on each line in the block.

Can I use triple quotes as Python comments?

You can use triple quotes to temporarily stop code from running, but they create a string rather than a comment. Use them for docstrings or multi-line text, and use # for actual comments.

What is the shortcut to comment multiple lines in Python?

Many editors use Ctrl + / on Windows and Linux, or Cmd + / on macOS. Select the lines first, then use the shortcut to add or remove # markers.

Will Python run code inside if False?

No, Python does not execute the block because the condition is always false. However, Python still checks the code for syntax errors before running the script.

Should I leave commented code in my Python project?

Keep small comments that explain important decisions, but remove large unused blocks after testing. Your version control history should store older code versions instead.

Commenting out multiple lines in Python is simple once you use # for every line and rely on your editor’s toggle shortcut. Start with standard comments for temporary testing, use triple quotes only for real multi-line strings or docstrings, and remove old disabled code when the script works. I hope you found this article helpful.

You May Also Like

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.