Multiline Comments in Python
Last Updated :
21 Feb, 2025
A multiline comment in Python is a comment that spans multiple lines, used to provide detailed explanations, disable large sections of code, or improve code readability. Python does not have a dedicated syntax for multiline comments, but developers typically use one of the following approaches:
It help to improve code readability, provide documentation, enhance collaboration, Aids in debugging.
Types of Multiline Comments
1. # symbol
Using multiple # symbols on separate lines is the most efficient and Pythonic way to write multiline comments.
Python
# This is a multiline comment
# Each line starts with #
# This method is efficient and preferred
print("Geeks For Geeks") # Inline comment
Explanation: Here, the first three lines contain a hash character(#) and the interpreter prevents the three lines from execution. Then it prints the "Geeks For Geeks" and finally, it will prevent the # line from execution.
2. Triple Quotes ('''
or """
)
Python allows the use of triple single (''') or triple double (""") quotes to define multi-line strings. Although these are technically string literals and not comments, they can be used as comments if they are not assigned to a variable.
Python
'''
This is a multiline comment using triple single quotes.
It is commonly used as a workaround.
'''
print("Triple Single and Double quotes")
"""
This is another multiline comment
using triple double quotes.
"""
OutputTriple Single and Double quotes
Explanation:
- These triple-quoted strings are ignored by Python if not assigned to a variable.
- This method is widely used for docstrings (described below).
3. \ method
Backslash Method for commenting out multiple lines in Python is an unconventional and lesser-known approach. It involves using the line continuation character (\) to extend a statement across multiple lines, effectively preventing Python from executing the code.
Example: In Python, a backslash (\) is used to indicate that a statement continues on the next line. If you use a backslash at the end of multiple lines without forming a valid statement, Python will treat it as incomplete and ignore it.
Python
# Using backslash for multiline comments
# This is a long comment \
# that spans multiple lines \
# using the backslash continuation method.
# Code continues below
print("geeks for geeks!")
4. Docstrings
A docstring is a special type of multiline string used to describe a module, function, class, or method. Unlike regular comments, docstrings are stored as metadata and can be accessed using the __doc__ attribute.
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.
Python
def docstring():
"""This is a docstring.
It describes what the function does.
"""
print("geeks for geeks tutorial")
print(docstring.__doc__) # Access the docstring
OutputThis is a docstring.
It describes what the function does.
Explanation:
- The function docstring() contains a docstring, which is a multi-line string enclosed in triple double quotes (""").
- The line print(docstring.__doc__) accesses and prints the docstring associated with the function.
- The docstring maintains its indentation as written in the function.
Difference between Docstring and Multiline Comments
Feature | Multiline Comments | Docstring |
---|
Purpose | used to add comment and explain code | used to document function, classes and modules.. |
---|
Syntax | use # on each line or triple quotes | uses triple quotes at the beginning of a function/class |
---|
Execution | ignored by python completely | stored as a string and accessible via .__doc |
---|
Usage | anywhere in the code | typically at the start of functions, classes, or modules |
---|
Retrieval | cannot be accessed at runtime | can be accessed using help() or .__doc__ |
---|
Best practice | use # for actual comments | use triple quotes for documentation |
---|
Multiline Comments for Debugging
A common use of multiline comments is temporarily disabling code for debugging.
Python
# print("This line is executed")
# print("This line is commented out")
"""
print("This block is commented out")
print("No Output Generated)
"""
Explanation:
- Useful for disabling code temporarily.
- # comments are better for debugging (they do not occupy memory).
- Triple-quoted strings should not be used for debugging.
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 cre
3 min read
Docopt module in Python Docopt is a command line interface description module. It helps you define a interface for a command-line application and generates parser for it. The interface message in docopt is a formalized help message. Installation You can install docopt module in various ways, pip is one of the best ways to
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
4 min read
Multi-Line printing in Python We have already seen the basic use of print function previous article. Now, let's see how to use print function for multi-line printing. This can easily be done using multiline string i.e. three single quotes ''' Geeksforgeeks ''' . Let's see different examples to see the demonstration for the same.
1 min read
How to Call Multiple Functions in Python In Python, calling multiple functions is a common practice, especially when building modular, organized and maintainable code. In this article, weâll explore various ways we can call multiple functions in Python.The most straightforward way to call multiple functions is by executing them one after a
3 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 typ
2 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 mai
4 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 availab
3 min read
Python end parameter in print() In Python, the print() function, commonly used for displaying output, by default ends each statement with a newline character (\n), but this behavior can be customized using the end parameter, which allows you to specify a different string (such as a space, comma, or hyphen) to be printed at the end
3 min read
Statement, Indentation and Comment in Python Here, we will discuss Statements in Python, Indentation in Python, and Comments in Python. We will also discuss different rules and examples for Python Statement, Python Indentation, Python Comment, and the Difference Between 'Docstrings' and 'Multi-line Comments. What is Statement in Python A Pytho
7 min read