Mastering Python Multiline Comments

Mastering Python Multiline Comments

3 mins read60 Views Comment
Updated on Oct 3, 2023 13:59 IST

Master the art of adding Python multiline comments to enhance code readability. Utilize triple quotes (โ€โ€™ โ€โ€™ or โ€œโ€โ€ โ€œโ€โ€) to enclose comments spanning multiple lines. This practice aids in providing clear, comprehensive explanations within your code, ensuring seamless collaboration and maintenance. Elevate your coding standards by integrating effective multiline commenting techniques in Python.

2023_10_Copy-of-Feature-Image-Templates.jpg

A comment in Python is a piece of text that is meant to be an explanatory or descriptive annotation in the source code and is not to be considered by the compiler/interpreter while generating machine language code.

There are two types of comments in Python:

  • Single-line comments start with the hash symbol (#) and extend to the end of the line.
  • Multi-line comments start with three double quotes (โ€œโ€โ€) and end with three double quotes (โ€œโ€โ€).

Note: The Python interpreter ignores any text between a commentโ€™s opening and closing delimiters.

Here is an example of comments in Python:

 
# This is a single-line comment.
"""
This is a multi-line comment.
It can span multiple lines and include
code snippets.
"""
# This is a comment that explains the purpose of the following line of code:
x = 2 + 3
# This is a comment that documents an assumption:
# Assume that the variable `y` is always positive.
y = 10
Copy code

What is a Python Multiline Comment?

In Python, a multiline comment is a way to include explanatory text in the code that spans multiple lines. Python does not have a specific syntax for multiline comments, but developers commonly use a workaround to achieve this.

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

โ€“ / โ€“
3 months
Free
3 hours
Free
19 hours
โ€“ / โ€“
4 months
Free
2 hours
Free
9 hours
โ‚น3 K
3 weeks
โ€“ / โ€“
โ€“ / โ€“
โ‚น3 K
10 hours

How to Create Multiline Comments in Python/Types of Multi-line Comment in Python

Triple Quotes

  • You can use triple single quotes (โ€โ€™) or triple double quotes (โ€œโ€โ€) to create a multiline comment.
  • Even though triple quotes are used for multiline strings, they can be used as multiline comments if the string is not assigned to any variable and is not part of an expression.
  • The Python interpreter ignores this string, effectively treating it as a comment.

Syntax

 
'''
This is a multiline comment.
It spans multiple lines.
'''
Copy code

Now letโ€™s take an example to get a better understanding of how to use triple quote multiline comment in python.

 
'''
This advanced example demonstrates the use of multiline comments
in a Python function that performs a complex calculation.
The comment provides a detailed explanation of the calculation logic,
parameters used, and the expected output.
'''
def complex_calculation(x, y):
# Complex calculation logic here
pass
Copy code

Using Hash (#) Symbol

  • Another way to create multiline comments is by placing a hash (#) symbol at the beginning of each line.
  • This method is more manual as you have to add a # for each line of the comment.
  • Syntax:
 
# This is a multiline comment.
# It also spans multiple lines.
Copy code

Now letโ€™s take an example to get a better understanding of how to use hash symbol for multiline comment in python.

 
# This is an example of a multiline comment
# that explains a complex algorithm or logic.
# Each line begins with a hash (#) symbol,
# and the comment can span across multiple lines.
def complex_calculation(x, y):
# Complex calculation logic here
pass
Copy code

Sometimes, we get confused by multiline comments and docstring in python. Both looks same but they are different. In the below table have briefly discussed how multiline comments and docstring are different from each other.

Difference Between Multiline Comment and Docstring in Python

Parameter Multiline Comment Docstring
Purpose Used for leaving comments in the code for understanding and readability. Used for providing documentation for functions, classes, modules, and methods.
Syntax Enclosed within triple quotes (โ€โ€™ or โ€œโ€โ€) Enclosed within triple quotes (โ€โ€™ or โ€œโ€โ€) , placed at the beginning of a function, class, or module.
Accessibility Cannot be accessed at runtime as it is ignored by the Python interpreter. can be accessed at runtime using the _doc_ attribute of the object (function, class or module)
IDE Support IDEs do not provide special support for extracting or displaying multiline comments. IDEs and documentation tools can automatically extract and display docstrings.
PEP 8 Guidelines No specific PEP 8 guidelines for multiline comments. PEP 8 provides specific guidelines for writing docstrings, ensuring consistency and standardization.
Use in Documentation Not used for automatic documentation generation. Used for automatic documentation generation using tools like Sphinx.
Effect on Code Execution No effect on code execution as it is treated as a comment. No direct effect on code execution, but accessible as metadata for providing information about the object.
About the Author