Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Comment Out a Block of Code in Python

  • 11 Jul, 2025
  • Com 5
Python Comment Block

To comment out a block of code in Python, prepend each line with a # (octothorpe) or enclose the block within triple quotes ( ‘ ‘ ‘ or ” ” “).

Single and Multiline comment block in Python

 

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

Multi-line strings as comments in Python

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:

  1. VS Code: Ctrl+/ (Windows/Linux) or Cmd+/ (Mac)
  2. PyCharm: Ctrl+/ (Windows/Linux) or Cmd+/ (Mac)
  3. 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!

Post Views: 35
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Numpy.nan (np.nan) in Python
How to Check If Variable is None in Python

5 Comments

  1. Suzanne Slayden

    May 10, 2022 at 9:24 pm

    nice post. very impressive well written

    Reply
  2. Katharine Sidhom

    May 15, 2022 at 7:58 am

    some genuinely interesting information, well written and broadly speaking user friendly.

    Reply
  3. Bharath

    June 15, 2022 at 2:53 pm

    very appreciatable Article ,

    Thanks and Reg,
    U.Bharath

    Reply
  4. Teodora Cripps

    July 14, 2022 at 2:08 pm

    But wanna comment on few general things, The website style and design is perfect, the written content is rattling superb : D.

    Reply
  5. robot final

    August 24, 2022 at 2:16 pm

    thank you so much for this

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend