Comments are used to enhance readability and understandability of source code by providing proper information about the code. Comments are not executed by an interpreter or compiler and they are used in the code only for assistance of the programmer. In this article, we will understand how to create multiline comments in python.

What is a multiline comment in python?

As we can see in the name itself, a multiline comment is a python comment which expands to multiple lines i.e. multiline comments are those comments which expand to two or more lines in the source code. Theoretically speaking, there is no syntax for multiline comments in python but we can implement multi line comment using single line comment or triple quoted strings. We will look at both methods to implement multiline comments one by one.

How to create multiline comments in python using # symbol?

As we know that single line comment in python are implemented by adding # symbol before the comment and it terminates whenever a line break occurs, we can put a # symbol at the start of each line of a multiline comment. In this way we can implement multi line comments using Single Line comments.This can be seen as follows. The function in the example adds a number and its square to a python dictionary as key-value pair.

#This is a multiline comment in python
#and expands to more than one line 

def add_square_to_dict(x,mydict):
    #This is an example of block of code where the block consists of an entire function
    a=x*x
    mydict[str(x)]=a
    return mydict

We can start a single line comment in python after any statement in the source code. In the similar way when we are implementing multiline comments using # symbol, we can start a multi line comment after any statement in the code.

#This is a multiline comment in python
#and expands to more than one line
print("Pythonforbeginners.com") #This is also a python comment
#and it also expands to more than one line.

How to create multiline comments in python using multiline strings?

To use multiline strings as multiline comments in python, we can use a multi line string without assigning it to any variable. In this way, when the interpreter will execute the code, no byte code will be generated for the unassigned string  as no address can be assigned to it. In this way, a multiline string will act as a multiline comment. This can be seen as follows.

"""This is 
a 
multiline comment in python
which expands to many lines"""
print("PythonforBeginners.com")

We can start a multiline comment after a statement in python when we implement multi line comments with # symbol. But we cannot start multi line comments after any statement if we are using multiline strings to implement multiline comments . If done, it will cause error. This can be seen as follows.

#This is a multiline comment in python
#and expands to more than one line
"""This is 
a 
multiline comment in python
which expands to many lines"""
print("Pythonforbeginners.com") """This is not 
a 
multiline comment in python
and will cause syntax error"""

When we implement comments using # symbol, we do not have to care about indentation but when we use multiline strings as comments, we have to follow proper indentation otherwise errors will occur. This can be seen as follows.

             #This is a multiline comment in python
#and expands to more than one line wthout caring for indentation
"""This is 
a 
multiline comment in python
which expands to many lines and should follow indentation"""
print("Pythonforbeginners.com")

Conclusion

In this article, we have seen the various ways to create multiline comments in python. We also saw how to use multiline comments so that they do not cause error in the program. Stay tuned for more informative articles.

Leave a Reply

Your email address will not be published. Required fields are marked *