Multiple-line Comment In Python with Best Examples

In this tutorial on multiple-line comments in Python, I’ll not only show you how to make single-line comments, but I’ll also show you how to make multiple-line comments.

What is comment in Python?

Comments in Python are the addition of brief explanations to code to improve its readability. A developer uses them to keep track of how he or she thinks while writing code. 

It illustrates the fundamental reasoning behind why a particular piece of code was developed.

There are two types of comments in Python: Single line Comment and Multiple-Line Comment.

How do you comment out multiple lines in Python?

In Python, you can comment out more than one line by putting a hash (#) in front of each line.

What is the shortcut to comment multiple lines in Python?

In any Python IDE, we can use CTRL+/ to comment out the lines of code we have chosen. To remove the comments from the chosen lines, we just have to press CTRL+/ again.

You can also try the shortcut to comment in our Online Python Compiler.

How do you comment a whole code in Python?

Using the hashtag (#) character is the easiest way to comment out a block of code in Python. The Python compiler will treat anything that starts with a hashtag as a comment.

You can block as many comments as you want, whether they are all in a row or not. This can help if we want to comment on more than one line.

Python comment block

To make a comment block in Python, add a hashtag (#) to the beginning of each line. Then, put a comment block in the code to stop it from running while you’re testing it.

How to comment code in Python?

To add a comment to Python code, write hashtag (#) at the beginning of each new line of code. It tells the Python compiler to skip over that line and move on to the next one. Comments start with a hashtag (#) and won’t be run by the Python compiler.

# The is a single line comment
x, y, z = 1, 2, 3
sum = x + y + z
print(sum)  # You can also comment after the code



In the above example, we added notes to the code with the # sign. The hashtag (#) only works with one line of code, not with more than one line.

If you put the comment at the end of the line, the Python interpreter runs everything before the comment and ignores everything after it.

Python Multi-line comment

Python does not have a built-in way to write comments with more than one line. To block comments on multiple lines in Python, add a hashtag (#) to the front of each line.

That means writing multiple single-line comments one after the other. If you start each line with the hashtag (#) in a row, you can make comments that span more than one line.

Consecutive single line comments

A line of Python code can be commented on with a hashtag (#). Comments don’t have to be text to explain the code; they can also be used to stop Python from running code.

Place the hashtag (#) before the line you want to comment on. In Python, single-line comments that come one after the other can be used as multiline comments.

# The proper way to use a
# multiple comment is by using 
# multiple single-line comments.

x, y, z = 1, 2, 3
sum = x + y + z



Here, the first three lines have a hashtag (#), which stops the interpreter from running those three lines. Then it prints the total of all the numbers.

Using multi-line strings as comments

Multiple-line comment in Python or string literals is a piece of text that is surrounded by three double quotes (""") at the start and end of each comment.

They are helpful when the text of a comment is too long to fit on one line and needs to go across multiple lines. Comment lines or paragraphs with more than one line serve as documentation for people who read your code.

Check out the following piece of code to see how multi-line comments work:

''' This is also a multiple-line comment.
You can also use 3 double quotes. '''
x, y, z = 1, 2, 3
sum = x + y + z



In the example above, comments on multiple lines of code are made with multi-line comments. Triple quoted strings (''') or triple double-quoted strings (""") can be used to comment on the first and second lines.

But if these multiline comments are put right after the signature of a function or class, they become document strings or also known as docstrings.

Docstring is a built-in part of Python that lets you link written documentation to Python modules, functions, classes, and methods. It is added right below the functions, modules, or classes to explain what they do. The Python __doc__ attribute is then used to show the docstring.

def sum(x, y, z):
    """Sum of x, y ,z"""
    return x + y + z

# The code below will print the docstring of sum function


Sum of x, y ,z


Even though Python doesn’t support multiline comments the way that some other programming languages do, you can still use the # character to make these kinds of comments.

Block comments are a common way to make comments with more than one line in Python. They can also be used to tell a program to ignore a block of code.

If block comments aren’t enough, you can also use docstrings to make a multiline comment in Python. Docstrings won’t make code unless they’re used in certain ways.

I hope you understand very well how to use multiple-line comments in Python.

Leave a Comment