In this article, we will delve into the concept of multiline comments in Python, providing a comprehensive definition along with illustrative examples in the Python programming language on How to Comment Multiple lines in Python.
What is a Multiline Comment in Python?
Multiline comments in Python refer to a block of text or statements that are used for explanatory or documentation purposes within the code. Unlike single-line comments denoted by the hash symbol (#), multiline comments are enclosed by triple double quotes (`”””`) or triple single quotes (`”’`). These comments are often utilized to provide detailed explanations, documentation, or notes about the code, and they can span multiple lines. While Python doesn’t have a specific syntax for multiline comments, using triple quotes achieves a similar effect, as the interpreter ignores the enclosed text.
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
- Using Backslash Method
Comment Out Multiple Lines in Python using 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: Consecutive Single-Line Comment
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.
Comment Out Multiple Lines in Python 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: Using a Multiline String as a Comment
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.
Comment Out Multiple Lines in Python using Backslash Method
A method to create multiline comments in Python involves using the backslash (`\`) at the end of each line to utilize the line continuation feature, thereby allowing the comment to extend to the next line. This line continuation method is less common than other approaches such as consecutive single-line comments or multiline strings and is primarily employed for code readability purposes when it is necessary to break a comment across multiple lines.
Example : In this example, the comments starting with
# are extended to multiple lines using the backslash (
\) at the end of each line. The backslash indicates that the comment continues on the next line. This method can be helpful for improving code readability when you have lengthy comments.
that spans multiple lines \
using the backslash continuation method.
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: Docstrings in Python
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.
Multiplies the value of a and b
Difference between Comments and Docstring in Python
Let us see the difference between Python comments and Docstring:
|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__
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!