The Wayback Machine - https://web.archive.org/web/20240710024428/https://www.geeksforgeeks.org/multiline-comments-in-python/
Open In App

Multiline comments in Python

Last Updated : 04 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Python3
# Write Python3 code here
# Single line comment used

print("Python Comments")

# print("Mathematics")

Output:

Python Comments

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. 

Python3
# Write Python code here

""" Multi-line comment used
print("Python Comments") """

print("Mathematics")

Output:

Mathematics

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.

Python3
# Using backslash for multiline comments
# This is a long comment \
# that spans multiple lines \
# using the backslash continuation method.

# Code continues below
print("Hello, World!")

Output :

Hello, World!

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.

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 codeGives a description of the Python modules, functions, and classes
They cannot be accessed They can be accessed using __doc__ 


Similar Reads

How do we create multiline comments in Python?
Comments are pieces of information present in the middle of code that allows a developer to explain his work to other developers. They make the code more readable and hence easier to debug. Inline Comment An inline comment is a single line comment and is on the same line as a statement. They are created by putting a '#' symbol before the text. Synt
3 min read
Multiline String in Python
A sequence of characters is called a string. In Python, a string is a derived immutable data type—once defined, it cannot be altered. To change the strings, we can utilize Python functions like split, join, and replace. Python has multiple methods for defining strings. Single quotations (''), double quotes (" "), and triple quotes (''' ''') are all
4 min read
Proper Indentation for Multiline Strings in Python
In Python, indentation plays a crucial role in code readability and structure, especially with multiline strings. Multiline strings, defined using triple quotes (""" or '''), allow for strings that span multiple lines, preserving the formatting within the quotes. Proper indentation of these strings is important for maintaining code readability and
3 min read
How to create a multiline entry with Tkinter?
Tkinter is a library in Python for developing GUI. It provides various widgets for developing GUI(Graphical User Interface). The Entry widget in tkinter helps to take user input, but it collects the input limited to a single line of text. Therefore, to create a Multiline entry text in tkinter there are a number of ways. Methods to create multiline
3 min read
How to make a kivy label multiline text?
Kivy is an open source software library for the rapid development of applications equipped with novel user interfaces, such as multi-touch apps. Using Kivy on your computer, you can create apps that run on: Desktop computers: OS X, Linux, Windows.IOS devices: iPad, iPhone.Android devices: tablets, phones.Any other touch-enabled professional/home br
3 min read
Interesting Fact about Python Multi-line Comments
Multi-line comments(comments block) are used for description of large text of code or comment out chunks of code at the time of debugging application.Does Python Support Multi-line Comments(like c/c++...)? Actually in many online tutorial and website you will find that multiline_comments are available in python(""" or '''). but let's first look at
3 min read
How to Extract YouTube Comments Using Youtube API - Python
Prerequisite: YouTube API Google provides a large set of API’s for the developer to choose from. Each and every service provided by Google has an associated API. Being one of them, YouTube Data API is very simple to use provides features like – Search for videosHandle videos like retrieve information about a video, insert a video, delete a video et
2 min read
Python Comments: Importance, Types and Correct Way to Use
Comments in Python are the lines in the code that are ignored by the interpreter during the execution of the program. # I am single line comment """ Multi-line comment used print("Python Comments") """ Comments enhance the readability of the code and help the programmers to understand the code very carefully.
4 min read
How to write Comments in Python3?
Comments are text notes added to the program to provide explanatory information about the source code. They are used in a programming language to document the program and remind programmers of what tricky things they just did with the code and also help the later generation for understanding and maintenance of code. The compiler considers these as
4 min read
Comments in Ruby
Statements that are not executed by the compiler and interpreter are called Comments. During coding proper use of comments makes maintenance easier and finding bugs easily.In Ruby, there are two types of comments: Single – line comments.Multi – line comments. Here, we are going to explain both types of comment with their syntax and example: Ruby Si
2 min read
three90RightbarBannerImg