Multiline Comment Python: how to comment multiple lines in python

What are the comments?

Comments are a piece of information that is written alongside programming code to explain the nature of the code and make it more readable. If you want to notify others what a certain piece of code does, you can do this using comments.

Comments are show-piece in the eyes of the compiler, which means when the compiler runs the code, it neglects the comments. Comments can be of two types, single-line comments where we write the comments in a single line, and multi-line comments where we consume multiple lines for saying in a comment.

Single-line Comments in Python

In Python, a single-line comment starts with a # sign. After that what you will write becomes comments. See the example below:

#The below prints something
print('Hello Python')
Output: Hello Python

As you can see, the first line has not had any impact on our output. Only the second line is executed while the first line is neglected by the compiler. The first line is a comment which is used here to notify that the below-coming code prints something.

Take another example:

#This is Python Code
#We are learning Comments
#The below code prints something
print('Hello Monty Python')
Output: Hello Monty Python

As you can see above, we have used single-line comments but multiple times. This can also be used if you required for multiline commenting. But Python has a separate method for multiline commenting.

Multiline Comments in Python

Since Python will ignore string literals that are not assigned to a variable, you can add a multiline string (triple quotes) in your code, and place your comment inside it.

As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and you have made a multiline comment. Let’s see below:

This is a multiline 
 written in
more than just one line
print("Hello Python")
Output: Hello Python

As you can see only the last line gets executed here and the remaining all other lines get neglected by the compiler as they are multiline comments here in Python.


Python Basics
List, Set, Tuple, and Dictionary in Python
Python Reverse List items
Python Round() Function
Python Multiline comment
Power in Python | Python pow()
Python range() Function
Square Root in Python
Python for i in range