To comment out a block of code in Python, prepend each line with a # (octothorpe) or enclose the block within triple quotes ( ‘ ‘ ‘ or ” ” “).
Single-line comments (#)
To comment out a single line of code, simply prepend it with “#”.
# This is a single-line comment
print("This is 11th July, 2025")
# You can comment out executable code
# print("This won't execute")
x = 5 # Inline comments are also possible
Multiple single lines
If you want to comment multiple lines, you need to prefix each line with “#”.
# This is block comment
# Created using # symbol
# This is the perfect example
print("Block Comment in Python")
# Output: Block Comment in Python
Nested comments
Be careful when creating nested comments, as you don’t need to nest them if you prepend your line with #.
Python does not support nested comments. If you try to nest # comments, you are just prepending multiple # symbols, which is unnecessary and can make the code messy or confusing.
Bad practice:
# # This line is already commented # # x = 10 # # y = 20 # # print(x + y)
Good practice:
# This line is already commented # x = 10 # y = 20 # print(x + y)
The code below is also considered bad practice:
def func():
"""
This is a docstring
"""
# Incorrect use below will break:
# """
# print("Hello")
# """
Multi-line strings as comments
You can use the triple-quoted(”’ or ” ” “) strings to create multi-line string literals, but you can also use them to comment out large blocks of code.
"""
This is a multi-line comment
Using triple-quoted string literals
In Python
"""
print ("Multi-line comments using triple quotes")
Triple quotes create string objects in memory. So, it is inefficient compared to #. Tools like linters or IDEs might misinterpret them as docstrings.
IDE-Specific Block Commenting
Most Python IDEs provide shortcuts for block commenting:
- VS Code: Ctrl+/ (Windows/Linux) or Cmd+/ (Mac)
- PyCharm: Ctrl+/ (Windows/Linux) or Cmd+/ (Mac)
- Sublime Text: Ctrl+/ (Windows/Linux) or Cmd+/ (Mac)
These shortcuts automatically add # to the beginning of each selected line.
Using if False (alternative to comment)
What if you don’t want to use # or “”” and use a programmatic approach? Well, that’s where if False comes into the picture.
When you set it to False, it deliberately ignores the rest of the code inside the if False because it will never execute since the if condition is always False.
It is a smart way to skip the unnecessary code.
# This has no runtime cost
if False:
print("Programmatical comment that will never execute")
Docstrings vs. Comments
Don’t confuse docstrings with comments. They both have different purposes.
The main difference between docstrings and comments is that docstrings are used to explain what a function/module/class does, while comment is used to explain how or why specific code works.
Docstring (for Documentation)
def add(a, b):
"""
Adds two numbers and returns the sum.
Parameters:
a (int): First number
b (int): Second number
Returns:
int: Sum of a and b
"""
return a + b
print(add.__doc__) # Accessing the docstring
Comment (for Code Explanation)
# Initialize the sum
final_value = 0
# Loop through numbers 1 to 5
for i in range(1, 6):
final_value += i # Add current number to total
print(final_value)
# Output: 15
That’s all!



Suzanne Slayden
nice post. very impressive well written
Katharine Sidhom
some genuinely interesting information, well written and broadly speaking user friendly.
Bharath
very appreciatable Article ,
Thanks and Reg,
U.Bharath
Teodora Cripps
But wanna comment on few general things, The website style and design is perfect, the written content is rattling superb : D.
robot final
thank you so much for this