Latest Code Tutorials

Python comment block: How to Write Multi-line Comments


In computer programming, a comment is a programmer-readable explanation in the code of a program. This is the same for every programming language. Comments are an integral part of any program. 

Comments in Python can be used to make the code more readable. Python also supports multi-line comments the way other languages do.

Python comment block

To create a comment block in Python, prepend a #(octothorpe) to each line. Use the comment block in Python code to prevent the execution while testing code.

Most existing programming languages have syntax for block comments that cross multiple text lines, like C or Java. Most Python IDEs support a mechanism to do the block-commenting-with-pound-signs automatically for you.

Programmers often use the comment blocks for mostly two purposes.

  1. Explaining the code
  2. Testing the code

How to comment code in Python

To comment code in Python, write the “#”(octothorpe) at every new code line. It tells the Python compiler to ignore its execution and go to the next line.

Comments start with a #, and the Python compiler will ignore its execution.

# Use "#" to comment in Python code
print("Comment in Python")

In the above example, we commented on the code using the # sign. The # works with only a single line of code and not multi-lines of code.

When you place a comment on the same line as a statement, you will have an inline comment.

Let’s use another example.

# Use * for multiplication of a number 
print(3 * 7)



If you put the comment at the end of the line, then the Python interpreter executes the content before the comment, and it will ignore after comment.

How to Write Multi-line Comments in Python

Python does not have any built-in mechanism for writing multi-line comments.

If you are working with Java or C, C++, you can write the following code for multi-line codes.

This is a block comment.
which you can write for multiple lines

But you can not do this in Python.

To write multiline comments in Python, prepend a # to each line to block comment. That means write Consecutive Single-line Comments. Start every line with # sign consecutively, and you will achieve multi-line comments.

Consecutive Single-line Comments

To comment on multiple lines of code in Python, use the consecutive single-line comments using #. The ‘#’ is called an octothorpe. Python doesn’t support proper multiline comments because it takes more effort to comment out multiple code lines.

# Use * for multiplication of a number
# The * is a multiplication operator in Python
# To print the value, use the print() function
print(3 * 7)



So, to add a multiline comment, you could insert a # for each line.

Using Multi-line Strings as Comments

To create multi-line comments in Python, another way is using multi-line strings or docstrings. This is used for documentation strings, not block comments, although it has a similar effect. If you are commenting things out temporarily, then it is fine as a temporary measure.

Python provides two kinds of docstrings: one-line docstrings and multi-line docstrings. We will use multi-line docstrings to create a block comment.

Use * for multiplication of a number
The * is a multiplication operator in Python
To print the value, use the print() function
print(3 * 7)



One-line docstrings in Python

A one-line docstring fits into one line. A one-line docstring in Python begins with triple quotes (“””) and also ends with triple quotes (“””).

""" Use * for multiplication of a number """
print(3 * 7)



As you can see, we have used triple-quoted strings to create something that resembles a multiline comment in Python. You need to make sure you indent the first “”” correctly; otherwise, you will get a SyntaxError.

It’s always considered a great practice to keep your comment clear, concise, and explanatory.

The ultimate goal of comment is to save time and energy for you and other developers with whom you collaborate in the project.

That is it for the Python comment block tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.