Introduction to Python Multiline Comment
It has been in practice for decades for the developers to add comments in the program to provide instructions on what the particular block of code is supposed to be doing when executed. Only single-line comments are accepted in python, 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 does Python Multiline Comment work?
Comments in the program are words meant to help users understand the program in English, which is easily readable. In python, the hash character (#) is used to make a comment at the start of the line. 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 alignment in Python; the indentation works instead of placing curly braces for defining functions. So while writing the comments, we should be careful about indentation. You might add a note before the function body but inside the function, also known as docstring, similar to multi-line comments that don’t have indentation. So the need for comment indentation and function blocks must be appropriately indented.
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 start with a # character in the comment line in the above program. And below is the program code to print the statement. 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 blue comment lines; 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 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 comments 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: The above example describes the use of multiline comments, which are described within triple quotes. Below is the screenshot that shows the example for multiline, which is 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, 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 how to declare docstring, 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 and 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, docstring can be declared within triple quotes but within the function, module, method, or class, which is similar to the multiline comment.
This is a guide to the Python Multiline Comment. Here we have discussed the introduction, how python multiline comment works, and different examples and code implementation. You may also look at the following articles to learn more –