Multiline Comments in Python (original) (raw)

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.

**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. """

`

Output

Triple Single and Double quotes

**Explanation:

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

`

Output

This is a docstring. It describes what the function does.

**Explanation:

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

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:

**Note: In notebooks like Google Colab or Jupyter, a standalone triple-quoted string placed in a cell is treated as the cell’s final expression and displayed as output (just like a string literal). Running such code will print the string as output. In normal Python scripts, it won’t print unless you use print().