When I troubleshoot a reporting script, I often need to temporarily turn off a few lines without deleting them. Maybe an email notification sends too early, a file-writing step needs testing, or I want to isolate a function that causes an error.
To comment out a block of code in Python, add a # at the start of every line in that block. Python has no dedicated multi-line comment syntax, but this approach is simple, reliable, and works in every Python 3 project.
How to Comment Out a Block of Code in Python
A comment is text inside a Python file that Python ignores when it runs the program. Developers use comments to explain logic, leave notes, or temporarily disable code during testing.
Python uses the hash symbol (#) for comments. Everything after # on that line becomes a comment.
# This line will not run
print("Monthly report created")
To comment out a block of code in Python, put # before each line:
# report_name = "sales_report.csv"
# total_sales = 12500
# print(f"Report: {report_name}")
# print(f"Total sales: {total_sales}")
Python skips all four lines. This is the correct and standard way to disable a group of statements.
I recommend this approach when you want to test a local automation script, debug a calculation, or hold onto code for a short time. It keeps your intent obvious to anyone reading the file later.
If you are new to Python files and scripts, see how to create a Python file in Terminal and run small examples locally.
Why Python Has No Block Comment Syntax
Some programming languages offer a special syntax for multi-line comments. Python intentionally keeps comments simple: each commented line starts with #.
That design matches Python’s emphasis on readable code. Since Python depends heavily on indentation to define code blocks, explicit line comments help you see exactly what Python will ignore.
For example, this function calculates a total and prints a status message:
def create_report():
total_sales = 12500
print(f"Sales total: {total_sales}")
print("Report completed")
During testing, you may want to stop the output lines while keeping the calculation:
def create_report():
total_sales = 12500
# print(f"Sales total: {total_sales}")
# print("Report completed")
The function still runs and stores 12500 in the total_sales variable. It simply does not display the two messages. If variables feel unfamiliar, start with this guide to Python variables.
Use an Editor Shortcut for Multiple Lines
Adding # manually works for two or three lines. For a larger block, use your code editor’s comment shortcut instead.
Most Python editors let you select several lines and toggle comments with one keyboard shortcut:
- Windows/Linux:
Ctrl + / - macOS:
Cmd + /
For example, select this section of a small log-processing script:
log_file = "server.log"
print("Reading log file")
print("Checking for failed requests")
print("Saving results")
You can see the output in the screenshot below.

Then use the shortcut. Your editor changes it to:
# log_file = "server.log"
# print("Reading log file")
# print("Checking for failed requests")
# print("Saving results")
Use the same shortcut again to remove the comment markers.
This is faster and safer than typing # on every line. It also preserves indentation, which matters when you comment code inside a function, loop, or conditional statement. If your spacing changes accidentally, learn how to indent multiple lines in Python.
Pro Tip: I use the editor shortcut only for temporary testing. If I leave a large disabled block in a production script for more than a day or two, I either delete it or move it into version control. Old commented code quickly makes maintenance harder.
Comment Out a Block Inside a Function
Python uses indentation to group statements inside a function, if statement, for loop, or while loop. When you comment out a block, keep the indentation before #.
Here is a reporting function before commenting:
def send_daily_report():
report_ready = True
if report_ready:
print("Creating CSV file")
print("Sending email")
print("Writing audit log")
print("Script finished")
Suppose you want to test the function without sending output from the if block. Comment the inner lines like this:
def send_daily_report():
report_ready = True
if report_ready:
# print("Creating CSV file")
# print("Sending email")
# print("Writing audit log")
print("Script finished")
This code produces an error because the if report_ready: statement has no executable code left inside it. Comments do not count as Python statements.
Add the pass statement when you temporarily comment out the entire body of a block:
def send_daily_report():
report_ready = True
if report_ready:
# print("Creating CSV file")
# print("Sending email")
# print("Writing audit log")
pass
print("Script finished")
pass tells Python to do nothing while keeping the block structurally valid. Learn more about when to use the Python pass statement.
Should You Use Triple Quotes Instead?
You may see developers use triple quotes to disable several lines:
"""
print("Creating CSV file")
print("Sending email")
print("Writing audit log")
"""
This may look like a block comment, but it is actually a multi-line string. Python creates a string value and then does nothing with it.
The code often appears to work, but I do not recommend it for commenting out a block of code in Python. Triple-quoted strings serve a different job: they create multi-line text, such as documentation strings or message templates.
Here is a proper use of triple quotes:
email_message = """
Hello Team,
The daily sales report is ready.
Please review the attached file.
"""
print(email_message)
This script stores the message in a variable and prints it. Python treats it as data, not as a comment.
Using triple quotes to disable code creates confusion because another developer may assume the string has a purpose. It can also behave unexpectedly in certain locations, such as directly after a function definition, where Python treats it as a docstring.
Use # for comments. Use triple quotes for genuine multi-line strings. For more help with text values, read about single and double quotes in Python.
Use if False Only for Controlled Testing
Another way to prevent Python from running a code block is to place it inside if False::
if False:
print("Creating CSV file")
print("Sending email")
print("Writing audit log")
print("Testing completed")
You can see the output in the screenshot below.

Python skips the indented block because the condition is always False. Unlike comments, Python still parses the code inside the block and checks its syntax.
That difference makes if False: useful when you want to keep code formatted as executable Python during a short test. For example, you may want to verify that a disabled block has no syntax mistakes before you enable it again.
However, do not treat if False: as a replacement for comments. It adds executable structure to your script, may confuse readers, and can hide unfinished work. Use it only when you have a clear testing reason.
If Python reports an error after you re-enable a block, this guide on fixing invalid syntax in Python can help you find the issue quickly.
A Real-World Debugging Example
Imagine a script that reads sales data, calculates a total, saves a file, and sends a notification. You want to confirm the calculation before the file and notification steps run.
sales = [125, 210, 175, 90]
total = sum(sales)
print(f"Total sales: {total}")
# save_report(total)
# send_notification(total)
The script prints the calculated total but skips the two later functions. This lets you test the core logic without creating files or triggering messages.
Once the total looks correct, remove the # characters:
sales = [125, 210, 175, 90]
total = sum(sales)
print(f"Total sales: {total}")
save_report(total)
send_notification(total)
This workflow saves time when you build automation scripts. Test the smallest useful part first, then enable later steps one at a time.
Things to Keep in Mind
- Use
#for real comments: Python has no official block-comment syntax, so add#to each line you want Python to ignore. - Keep required blocks valid: Add
passif comments leave a function, loop, orifstatement with no executable body. - Avoid triple quotes for disabled code: Triple quotes create a string, not a comment, and can confuse future readers.
- Do not leave dead code forever: Delete old experiments after you finish testing, or rely on source control to recover them later.
- Preserve indentation: Commenting code inside nested blocks requires careful spacing, especially when you uncomment it.
- Test after uncommenting: Run the script immediately after restoring code, since a missing indent or stale variable can cause errors.
Frequently Asked Questions
How do I comment out multiple lines in Python?
Add # at the beginning of every line you want to disable. Select the lines in your editor and use Ctrl + / on Windows or Linux, or Cmd + / on macOS, to do this quickly.
Is there a block comment in Python?
No. Python does not provide a dedicated block-comment feature like /* ... */. Use consecutive # characters instead.
Can I use triple quotes as comments in Python?
You can use them in some cases, but you should avoid doing so. Triple quotes create a multi-line string, while # creates an actual comment.
Why do I get an IndentationError after commenting code?
You likely commented out every executable line inside a function, loop, or conditional block. Add pass at the correct indentation level, or leave one valid statement inside the block.
How do I uncomment multiple lines in Python?
Select the commented lines and use your editor’s comment shortcut again. The editor removes the leading # characters from each selected line.
Does Python run commented-out code?
No. Python ignores text after # when it executes a script. The interpreter does not run, calculate, or validate that commented code.
Commenting out a block of code in Python comes down to adding # to each line, preferably with your editor’s toggle-comment shortcut. Start with standard comments, use pass when a block must stay valid, and avoid using triple-quoted strings as fake comments. I hope you found this article helpful.
You May Also Like
- How to comment out multiple lines in Python
- How to define a function in Python
- How to use if-not conditions in Python
- How to use a for loop in Python
- How to check your Python version

Bijay Kumar is an experienced Python and AI professional who enjoys helping developers learn modern technologies through practical tutorials and examples. His expertise includes Python development, Machine Learning, Artificial Intelligence, automation, and data analysis using libraries like Pandas, NumPy, TensorFlow, Matplotlib, SciPy, and Scikit-Learn. At PythonGuides.com, he shares in-depth guides designed for both beginners and experienced developers. More about us.