How to Comment Out Multiple Lines in Python

In programming, commenting out a code is a common practice for various reasons, including debugging, documenting code, or temporarily disabling a few of the features. In Python commenting out multiple lines of code can help to improve the readability and maintenance of code to better organise it.   TL;DR These are quick Keyboard Shortcuts to…

By.

min read

In programming, commenting out a code is a common practice for various reasons, including debugging, documenting code, or temporarily disabling a few of the features.

In Python commenting out multiple lines of code can help to improve the readability and maintenance of code to better organise it.

 

TL;DR

These are quick Keyboard Shortcuts to comment out multiple lines in Python:

Windows: Ctrl + /

macOS: Cmd + /

Above shortcut works on

Visual Studio Code, Sublime Text, PyCharm, Atom

For Notepad++:

Windows: Ctrl + Q

 

 

Methods for Commenting Out Multiple Lines in Python

There are two main methods for commenting out multiple lines in Python:

 

1 – Using the Hash Symbol (#)

This method involves placing a hash symbol (#) at the beginning of each line you want to comment out.

While this is effective, it can be time-consuming for large blocks of code.

def example_function():
    # This is a single line comment
    # print("This line is commented out and won't execute")
    print("This line will execute")

example_function()

Use the hash symbol (#) for short or single-line comments.

 

2 – Using Triple Quotes (“”” or ”’)

An alternative to using the hash symbol is to enclose the code block within triple quotes (“”” or ”’).

This method is more efficient for commenting out large blocks of code, as it only requires adding the triple quotes at the beginning and end of the block.

def example_function():
    """
    print("This line is commented out and won't execute")
    print("This line is also commented out and won't execute")
    """
    print("This line will execute")

example_function()

Use triple quotes (“”” or ”’) for multi-line comments or large blocks of code.

 

 

Leave a Reply

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