Python Comment Block: How to Block Comment In Python.

In JavaScript, we use the forward slash and the asterisk (/*, */) to comment out a block of code. In C#, Java, Golang, Rust, etc., commenting out a block of code can be done using the same combination of symbols as in JS. In other programming languages, a block of code can be commented out using the chosen symbol; for example =begin and =end in Ruby, and {-Β and -} in Haskell.

Now the point I’m trying to make here is that almost every programming language has a way to comment out a block of code, only except for a very few. But for the very few, it is surprising to find that the big snake is on the list. Yep, Python does not offer a way to comment out a block of code natively. With all its popularity and easiness of use, it is kinda disappointing that we have to hassle our code in order to create a Python comment block.

When I say, β€˜a block of code,’ I mean a snippet of code that spans across multiple lines. In fact, when we talk about commenting out a block of code in Python and other programming languages, we have to think of multi-line comments. But unfortunately, as I said above, we cannot do that in Python.

Anyway, Python developers have come up with multiple solutions to block comment in Python. And these solutions, I will happily share with you in this blog post. I will share with you two methods of block commenting that most Python developers use, as well as keyboard shortcuts to comment out your Python code in two of the most popular Python text editors – VS Code and Pycharm. So, let’s dive right in!

PS: If you are also a Django developer, I’ve also written a post on how to add comments in Django.

Table of Contents

Quick Answer: How to Comment Block In Python.

To comment out a block of code in Python, You can either start each line of the block you want to comment with a pound sign (#) or you can wrap your block of code in three consecutive double or single quotes(''' or """) to instantly comment out that block of code. But as you continue in the next sections that explore Python comment blocks in much detail, you will see that there is one method of commenting out a block of code in Python that is not suitable for production use; so stay tuned.

Python Comment Block: How to Block Comment In Python.

What Is A Block Comment In Python?

In order to understand what a python comment block is, one must first understand what a block of code is.

So, to be precise, a block of code is a group of consecutive lines of code that perform a certain task together. Python maintains a uniform indentation level for code blocks. However, in general terms, a block of code can simply be lines of code found next to each other; whether they are working together or not. This then means a Python block comment is a snippet of consecutive lines of code that are marked in a certain way to indicate to a developer that the snippet will be ignored by the interpreter and to instruct the interpreter not to execute that snippet of code.

How to Block Comment In Python.

In this post, I will show you two ways to write block comments in Python; which are:

  1. Start each line of the code block with a pound sign (#)
  2. Wrap the code block in triple quotes (''' or """)

1. Start each line of the code block with a pound sign (#) to create a Python Comment Block

The pound sign(#), or sometimes the hashtag symbol is the one and only official symbol for single-line comments in Python (And all Python comments in general since we only officially have single-line comments). Placing it before a line of code will make whatever is in front of the sign in that same line a comment.

# This is a Python single-line comment and it will be ignored

Do you see the catch here? To make a block comment in Python, start each line of the code block with a pound sign.

# This is
# a python block comment
# created by starting each line
# with the pound sign
print ('This section is not a python block comment')

As you can see, creating a comment block by placing # at the start of each line can be a very long and tedious thing to do. In the next section, I will show you keyboard shortcuts you can use to create the same type of comment block in a much faster and more efficient way.

2. Wrap the code block in triple quotes (''' or """) to create a Python Comment Block

Another way to create a comment block in Python is to wrap the piece of code you want to comment out in triple quotes. This is a very shady way of creating a block comment because triple quotes in Python are officially used for docstrings which is a way of documenting a function or class.

"""
This is a
Python multi-line Docstring
which can also be used
to create a python block comment
Just like this one
"""
print ('This section is not a python block comment')

However, even though this may be the case, the type of block comment created here will be ignored by the interpreter just as starting each line of the code block with a pound sign. In the next section, I will talk about the best method to create a comment block in Python.

Which Method To Comment Block in Python should you Use.

Although using consecutive pound signs (#) at the start of each line in the block of code is slow and tiresome, it is actually the recommended way of commenting out a block of code in Python. Later in the post, I will share with you block commenting keyboard shortcuts you can use to make this process a little faster.

The problem with using triple quotes (''' or """) to block comment in Python is that this syntax was originally made for function docstrings which we use to document the use of a function in Python. So when you use the docstring syntax to create block comments, your code will obviously look unprofessional to other Python developers. Some will recommend you use this way of creating a block comment only when you still have the code by yourself. But I think it is better not to use docstrings to block comment at all, as a best practice.

FAQs on Python Comment Block:

These questions have been asked by fellow Python developers. They are related to python comment blocks or python block comments.

What is the shortcut to comment multiple lines in Python?

The two most popular text editors used for Python programming are VS Code and Pycharm. I will talk about the shortcut to create comment blocks in those text editors:

To create a Python block comment in VS Code, use the keyboard shortcut CTRL + / in Windows and Command + / on Mac. To uncomment, reselect the comment block and press the same keyboard shortcut.

On the other hand, to create a Python block comment in Pycharm, use the keyboard shortcut CTRL + / in Windows and Command + / on Mac. To uncomment, reselect the comment block and press the same keyboard shortcut.

What you have to do in both of these methods is to start by highlighting or selecting the piece of code that you want to turn into a Python comment block, then depending on the text editor you are using, press the suitable keyboard shortcut and that code block will be instantly changed into a Python comment block.

I’m pretty sure most text editors offer keyboard shortcuts to create Python comment blocks. If you happen to be not using the text editors whose comment block keyboard shortcuts I have shared with you, please do some research on that. You can do so simply by searching for something like β€˜python comment block keyboard shortcut for {text editor}’ on Google. You obviously know better.

Conclusion: How To Add Python Block Comments In Your Code.

If you have reached this far, it’s as good for you as it is for me. Thank you so much for stopping by. I really hope you have learned something important or new about python block comments. If want you have something to talk about this topic, you are so very welcome to do so in the comments section below. But for now, see you in other CodingGear articles. Peace!

Python Comment Block: How to Block Comment In Python.
Scroll to top