Introduction to Python Multiline Comment
It has been in practice for decades for the developers to add comments in the program in order to provide instructions on what the particular block of code is supposed to be doing when executed. In python, only single-line comments are accepted, which can be done using ‘#’. For making a multiline comment, one can declare any symbol like declaring a variable in a function and call the function for entering plain text wherever it is required in the program.
How Python Multiline Comment Works?
Comments in the program are a set of words meant to help users understand the program in the English language, which is easily readable. In python, the hash character (#) is used at the start of the line to make it a comment. There is no multiline comment in Python, but still, there is a practice of using triple quotes for declaring multiline comments.
It is important to see the alignment as in Python; the indentation works instead of placing curly braces for defining a function. So while writing the comments, we should be careful about indentation. For example, you might add a note before the function body but inside the function, which is also known as docstring, similar to multi-line comments that don’t have any indentation. But the function could have blocks of code indented at multiple levels. So the need for comment indentation as well as function blocks is necessary to be indented properly.
Let us see how to indicate single line comments in Python with the below example:
# Program to print the statement print "Hello Educba"
The blue color words starting with a # character in the comment line in the above program. And below is the program code to print the statement. This is a single-line comment that starts with the hash character (#) and ends with the line’s end. These single-line comments are used for short and quick notes to comment on the understanding of the program.
Multiline Comments in Python with Example
Multiline comments are wrong to use in Python because if there are any number of lines to comment they should start with a hash character (#) followed by one space for each line. Let us see in below example:
# To learn how to use comments # Python does not have any special symbol for multiline comments. # Program to print the statement print "Hello Educba"
Explanation: From the above example, there are 3 comment lines that are blue in color, therefore the python hash character (#) is used for both single and multiline comments, but in a multiline comment, hash character (#) is followed by space for declaring it as a multi-line comment.
In some practices in Python, multiline comments are indicated using triple quotes (“ “ “ ” ” ”). In Python, even docstring is also defined in triple quotes, but docstrings are usually defined inside a class, function, module, or method as a first statement. This docstring may appear as a multiline comment, so Python does not support multiline comment due to this ambiguity between multiline comment and docstring. As python compiler ignores the comment while executing the program, but in the case of the docstring, you can access them with the special variable myobj.doc.
''' To learn how to use comments Python does not have any special symbol for multiline comments. Program to print the statement ''' print "Hello Educba"
Explanation: In the above example, which describes the use of multiline comments, which is described within triple quotes. Below is the screenshot that shows the example for multiline, which is in green in color.
In Python, multiline comments are not supported because of docstring, which coincides with the multiline comment as the compiler considers them as they are described within function or class or modules. Docstring is not a comment; they are a python string documentation feature that allows adding quick notes within a function, class, or module.
def docFunction(): ''' This function is an example of docstring in Python. ''' print("Python docstrings are not multiline comments.") print("\ndocstring value...") print(docFunction.__doc__)
Explanation: The above example is for how to declare docstring, which is similar to multiline comment. In Python, as the comment is declared using the hash character for the single line and multiline, the practice for multiline comment is usually declared using triple quotes. Therefore always declare in the proper way of comments or docstring as they lead to quicker execution of the program as well as an easy understanding of the code for users.
In Python, comments are written to make users understand the program code or in readable code. Python does not support multiline comments as there is an ambiguity between comment and docstring. As both multiline and docstring are declared within the triple quotes (“ “ “ ” ” ”). In python, comments are declared starting with the hash character (#) and ending with EOL ( End of the line). Thus as docstring can be declared within triple quotes but within the function or module or method or class, which is similar to the multiline comment.
This is a guide to the Python Multiline Comment. Here we discuss the introduction and how python multiline comment Works along with different examples and code implementation. You may also look at the following articles to learn more –