Python Comments is used for the non-executable statement, it helps to make the code readable. And multiline comments in Python are multiple lines of comments.
Types of Multiline Comments in Python
There are two ways by which we can add Python multiline comments in our code. They are as follows:
- Consecutive single-line comment
- Using a Multi-line string as a comment
Consecutive Single-Line Comment
In Python, the hash character(#) is used to comment on the line. Single-line comments in Python do not have to be text alone to explain the code, they can also be used to prevent Python from executing code. The hash character should be placed before each line to be considered as multiline comments in Python.
Example:
Here, the first two lines contain a hash character(#) and the interpreter prevents the two lines from execution. Then it prints the “Python Comments” and finally, it will prevent the last line from execution.
Python3
# Write Python3 code here # Single line comment used print ( "Python Comments" ) # print("Mathematics") |
Output:
Python Comments
Using a Multiline String as a Comment
Python multiline comments can also be enclosed in a delimiter (“””). Again there should be no white space between delimiters (“””). They are useful when the comment text does not fit into one line, therefore needs to span across lines. This type of string literal gets ignored as it is not assigned to any variable. We can access these strings using __doc__.
Example:
Multi-line comments are used to comment on more than one line. The first line is a single-line comment. The second and third lines can be commented on using triple quotes(“”” “””). This prevents the execution of the above code. Finally, it prints “Mathematics” in the output. However, if these Python multiline comments are placed directly after a function or class signature, then these turn into docstrings.
Python3
# Write Python code here """ Multi-line comment used print("Python Comments") """ print ( "Mathematics" ) |
Output:
Mathematics
Docstrings in Python
The docstring is an in-built feature of Python, which is used to associate documentation that has been written with Python modules, functions, classes, and methods. It is added right below the functions, modules, or classes to describe what they do. In Python, the docstring is then made available via the Python __doc__ attribute.
Example:
In this example, after multiply() function is defined, we declared a docstring comment using the triple quotes. Then we are printing the docstring using the __doc__ attribute.
Python3
def multiply(a, b): """Multiplies the value of a and b""" return a * b # Print the docstring of multiply function print (multiply.__doc__) |
Output:
Multiplies the value of a and b
Difference between Comments and Docstring in Python
Let us see the difference between Python comments and Docstring:
Comments |
Docstrings |
---|---|
They are declared using # | They are declared using “”” “”” |
Used to increase the readability of the code | Gives a description of the Python modules, functions, and classes |
They cannot be accessed | They can be accessed using __doc__ |