How to Comment Out A Block of Code In Python.

A block of code is a snippet of code that spans across multiple lines. 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 interpreter will ignore the snippet and to instruct the interpreter not to execute that snippet of code. Python does not offer a way to comment out a block of code natively. This post will try to outline some ways to achieve this.

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.

If you are using VS Code or Pycharm, you can comment out a block of code by highlighting the snippet and pressing CTRL + / in Windows or Command + / on Mac. To uncomment, reselect the comment block and press the same keyboard shortcut.

Why Comment A Block of Code In Python?

Python Comment Block: How to Block Comment In Python.
  1. Debugging: When you’re trying to identify the source of an issue in your code, you can temporarily comment out certain code blocks to isolate the problem. This allows you to run the program without executing the commented-out code, helping you pinpoint where the issue lies.
  2. Testing: When you’re testing different variations of your code or experimenting with new functionality, you can comment out existing code blocks to prevent them from executing while you test the new code. This enables you to compare the behavior of different code versions easily.
  3. Temporarily disabling functionality: If you have a piece of code that you don’t want to execute in the current run but might need later, you can comment it out. This way, the code remains in your script for future reference, but it won’t be executed until you uncomment it.
  4. Documentation: You can use comments to provide explanations, notes, or reminders about what a particular block of code does. This can be helpful for others (or yourself) when revisiting the code later, making it easier to understand its purpose and functionality.

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 only official symbol for single-line comments in Python. Placing it before a line of code will make whatever is in front of the sign in that same line a comment.

To make a block comment, 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 and will be printed')

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 not the official way of creating a block comment because triple quotes in Python are officially used for docstrings (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.

The problem with using triple quotes (''' or """) to comment out blocks of code in Python is that they where originally made for function docstrings. 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.

Keyboard shortcut to comment out Python code blocks in VS Code and Pycharm?

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 comment out a block of code in VS Code, highlight the code snippet, and press the keyboard shortcut CTRL + / in Windows and Command + / on Mac. To uncomment, reselect the comment block and press the same keyboard shortcut.

To comment out a block of code 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.

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.

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

Stephen Mclin
Stephen Mclin

Hey, I'm Steve; I write about Python and Django as if I'm teaching myself. CodingGear is sort of like my learning notes, but for all of us. Hope you'll love the content!

Articles: 90