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
Chain Multiple Decorators in Python
In this article, we will try to understand the basic concept behind how to make function decorators and chain them together we will also try to see Python decorator examples. What is Decorator In Python?A decorator is a function that can take a function as an argument and extend its functionality an
2 min read
Python Naming Conventions
Python, known for its simplicity and readability, places a strong emphasis on writing clean and maintainable code. One of the key aspects contributing to this readability is adhering to Python Naming Conventions. In this article, we'll delve into the specifics of Python Naming Conventions, covering
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
Command Line Arguments in Python
The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments. Python provides various ways of dealing with these types of arguments. The three most common are: Table of ContentUsing sys.argvUsing getopt moduleUsing a
5 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
Multi-Line Comment in Shell Script
Comments are quite a crucial part of a program or a code-base. It helps in understanding the code, improves code-readability, and also helps in enhancing the structure of the program. We often write single-line comments in our programs as they are quite self-explanatory and require few words to desc
2 min read
Multithreading in Python
This article covers the basics of multithreading in Python programming language. Just like multiprocessing , multithreading is a way of achieving multitasking. In multithreading, the concept of threads is used. Let us first understand the concept of thread in computer architecture. What is a Process
8 min read