Skip to content

How to comment out a block of code in Python

In Python, you can comment out a block of code by using the "#" symbol at the beginning of each line. For example:

An example for commenting out single code lines in Python

python
# This is a comment
# print("This line of code will not be executed")
print("This is the main code and will be executed")

<div class="alert alert-info flex not-prose"> Watch a course Python - The Practical Guide</div>

Another way to comment out multiple lines of code at once is to use triple quotes """. This can be used for both single or multiple line comments.

An example for commenting out multt-line code in Python with three double quotations

python
"""
This is a multi-line
comment that can span multiple lines
"""
print("This is the main code")

Alternatively, you can also use the ''' for multiple line comments.

An example for commenting out multt-line code in Python with three single quotations

python
'''
This is a multi-line
comment that can span multiple lines
'''
print("This is the main code")

Please note that, comments have no effect on the execution of code, they are only for human readers to understand the code.

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.