How to block a comment in Python?
Tags: python, programming, tutorial, beginners
Published: 2022-04-21 06:22:51

Unlike other programming languages such as C, C++, and Java, Python does not support /\*......\*/ for multi-line comments or block comments, there is no built-in mechanism for block comments in Python. We have different conventions and syntax for block comment in Python. Here you will read about adding block comments to your code.

## Table of contents

1.  [Understanding the concept](#block-comment-in-python)
2.  [Using multiple single # line comments](#using-multiple-single-line-comments-to-add-a-block-comment-in-python)
3.  [Using triple-quoted string literals for block comments](#using-triplequoted-string-literals-to-create-block-comment-in-python)
4.  [Closing thoughts](#closing-thoughts)

## Block comment in Python

Comments are specially marked lines of text in the program that are not evaluated. There are usually two ways to comment in any programming language.  
<span id="section2"></span>

The first is known as a single-line comment because it only applies to a single line in the "source code"(the program) and the second type of comment is a Block comment, and it usually relates to a paragraph of text. A block comment has a start and an end sign, and the computer ignores everything in between.  

## Using multiple single # line comments to add a block comment in Python

The most common way to comment out a block of code in Python is using the # character. Any line of code starting with # in Python is treated as a comment and gets ignored by the compiler.  
Since only a line of code after the # character is considered a comment, so it is great for single-line comments, so you can use it as many times as you want in a single code.

### Input:

    # This is block comment
    # Made using # character
    # Used multiple times
    print ("Block Comment")

<span id="section3"></span>

### Output:

    Block Comment

## Using triple-quoted string literals to create block comment in Python

In order to write a proper block comment in Python, we can use triple-quoted syntax with multi-line strings. These strings should not be confused with Docstrings (triple-quoted string literals appearing right after a function/class/module to create documentation). If we use it to comment out multiple lines of code in Python, that block of code will be ignored, and only the lines outside the docstring will run.  
Despite the fact that triple quoted string literals do not generate code, they are handled as such and must be indented properly within blocks in order to function properly!

#### Input:

    """
    This is block comment
    Made using # character
    Used multiple times
    """
    print ("Block Comment") 

<span id="section4"></span>

#### Output:

    Block Comment

If you use 4 spaces (or a tab) for indentation, you will get an indentation error.

## Closing thoughts

Since there's no built-in support for multi-line [comments](https://www.pythonforbeginners.com/comments/comments-in-python) in Python, we can use a triple-quoted multi-line string for creating block comments. Still, you should generally stick to using regular Python comments using a # character, even if you have to use it for multiple lines. This is because docstrings are for documentation, and not for commenting out code. One can learn about more Python concepts [here](https://flexiple.com/python-get-current-directory/).