Python provides the single line and multiline comments in order to add some notes inside the python code. Multiline comment or Block comment consists of multiple lines of comments which are joined together like a block. In this tutorial, we examine how to create multiline or block comments in Python.
Multiline/Block Comment Syntax
The multliline or block comment in Python has the following simple syntax.
CODE #COMMENT #COMMENT #COMMENT CODE
- CODE is a Python statement that is executed by a Python interpreter.
- # is the comment sign which is located at the start of the line in multiline comments. # is used multiple times in a block level to create block comments. There is no restriction about the count of the comment lines which can be 3 or 13.
Multiline/Block Comment with Hash Mark
Lets make a simple example about multiline comments. In the following example we create a multiline comment which consist of 4 lines.
print("I am NOT a comment") # I am a comment # I am a comment # I am a comment # I am a comment print("I am NOT a comment")
Multiline/Block Comment with String Literals
String Literals are normally used to defined and assign multiline strings into variables or pass as parameters. But interestingly string literals can be used to create multi-line comments if they are not assigned into a variable. If a literal or value is not assigned into a variable it does not create an error. This makes the string literal to create comment which can be read by developer but do not affect program execution. The triple quotes are used to create a multiline comment at the start of the comment and at the end of the multiline comment.
a = ''' This is multiline string ''' ''' This is multiline or block comment ''' print(a)