As a developer working on a project for a US-based company, I recently encountered a situation where I needed to temporarily disable a large block of code for testing purposes. In this tutorial, let us understand how to comment out multiple lines in Python. Let us see how it works
Comments in Python
Before we get into commenting out multiple lines, let’s know what is comments in Python. Comments are lines of text in your code that are ignored by the Python interpreter. They are used to explain the purpose of your code, make it more readable, and help developers understand your thought process.
In Python, a single-line comment begins with a hash symbol (#), followed by the comment text. For example:
# This is a single-line comment in Python
print("Hello, John from New York!")Read Interfaces in Python
Comment Out Multiple Lines in Python
Python provides several ways to comment on multiple lines.
1. Use Triple Quotes in Python
One of the most convenient ways to comment out multiple lines in Python is by using triple quotes. Python supports both single (''') and double (""") triple quotes for creating multiline strings. When these multiline strings are not assigned to a variable or used as a docstring, they are treated as comments.
Here’s an example:
'''
This is a multiline comment in Python.
It can span across multiple lines without the need for special symbols.
The Python interpreter will ignore everything inside the triple quotes.
'''
def greet(name):
"""
This function greets the person passed in as a parameter.
"""
print(f"Hello, {name}! Welcome to our US-based company.")
greet("Amanda from California")I executed the above code and added the screenshot below, you can see how multiple comment looks.

In the code above, the text enclosed within the triple single quotes (''') is treated as a comment and ignored by the Python interpreter. The triple-double quotes (""") are used as a docstring for the greet() function, briefly describing what the function does.
Check out Access Modifiers in Python
2. Use Editor Shortcuts
Most Python IDEs and text editors provide shortcuts for quickly commenting out multiple lines of code. These shortcuts can save you time and effort, especially when dealing with large blocks of code. For example, in IDEs like PyCharm or Visual Studio Code, you can select the lines you want to comment out and use the following shortcuts.
- Windows/Linux:
- Single-line comment: Ctrl + /
- Block comment (multiple lines): Shift + Alt
+A (VS Code/PyCharm), Ctrl + Shift + / (Sublime) - Mac:
- Single-line comment: Cmd + /
- Block comment (multiple lines): Shift + Option + A (VS Code/PyCharm), Cmd + Shift + / (Sublime)
- Jupyter Notebooks:
- Windows/Linux:
- Single-line comment: Ctrl + /
- Mac:
- Single-line comment: Cmd + /
Let’s say you have the following code:
def calculate_tax(income):
if income <= 9875:
tax = income * 0.1
elif income <= 40125:
tax = 987.5 + (income - 9875) * 0.12
else:
tax = 4617.5 + (income - 40125) * 0.22
return tax
income = 50000
tax_amount = calculate_tax(income)
print(f"For an income of ${income}, the estimated tax is ${tax_amount:.2f}")If you want to comment out the entire calculate_tax() function, you can select those lines and use the appropriate shortcut for your operating system. The result will look like this:
# def calculate_tax(income):
# if income <= 9875:
# tax = income * 0.1
# elif income <= 40125:
# tax = 987.5 + (income - 9875) * 0.12
# else:
# tax = 4617.5 + (income - 40125) * 0.22
# return tax
income = 50000
tax_amount = calculate_tax(income)
print(f"For an income of ${income}, the estimated tax is ${tax_amount:.2f}")Read How to Use Single and Double Quotes in Python?
3. Use the pass Statement
Sometimes, you might want to temporarily comment out a block of code without removing it or adding comment symbols. Python provides the pass statement for such situations. The pass statement is a null operation, meaning it does nothing when executed.
You can use the pass statement to replace the code you want to disable temporarily. Here’s an example:
def process_data(data):
pass
# for item in data:
# if item.startswith("USA"):
# print(f"Processing {item}...")
# # Perform processing steps here
data = ["USA-New York", "France-Paris", "USA-California", "Germany-Berlin"]
process_data(data)In this case, the process_data() function’s code block is replaced with the pass statement, effectively disabling the code without removing it entirely. This can be useful when you want to test different parts of your program without deleting the code you’ve already written.
Check out How to Comment Out a Block of Code in Python
Conclusion
In this tutorial, I have explained how to comment out multiple lines in Python. I discussed what are comments in Python, commenting using triple quotes and using editor shortcuts, we also discussed how to comment using pass statements.
You may also like to read:
- Difference Between “is None” and “== None” in Python
- How to Comment Out a Block of Code in Python?
- Difference Between {} and [] in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.