Python Comment is a fundamental  tool for the software engineers. Comments are generally utilized to explain the code. We can easily get the code if it has a proper clarification. A good developer should utilize the comments because in the future anyone wants to modify the code as well as implement the new module; then, it can be done without any problem.

Python comments


There are mainly three types of comments in Python programming – 

  1. Single line Comments
  2. Multiline Comments
  3. Docstring Comments


Comments are generally utilized for the accompanying purposes: 

  1. Code Readability
  2. Clarification of the code or Metadata of the project
  3. Prevent execution of code
  4. To include resources


Types of Comments in Python Language with Example

There are three main types of comments in Python. They are: 


Single-Line Comments in Python

Python single line comment begins with the hashtag symbol (#) with no blank areas and lasts till the end of the line. If the comment exceeds one line then put a hashtag on the following line and proceed the comment. Python’s single-line comments are proved valuable for providing short clarifications for variables, function declarations, and expressions. See the accompanying code snippet showing single line comment:
Example: 


# printing a string
print('Hello world')


Output:-

Hello world


Multi-Line Comments in Python

Python doesn't give the choice for multiline comments. However, there are various ways through which we can compose multiline comments.


Utilizing Multiple Hashtags (#)

We can multiple hashtags (#) to compose multiline comments in Python. Every line will be considered as a single line comment.

Example: Multiline comments utilizing multiple hashtags(#)

# Python program to demonstrate
# multiline comments
print("Multiline comments")


Output:-

Multiline comments


Utilizing String Literals

Python ignores the string literals that aren't assigned to a variable so we can utilize these string literals as a remark

Example 1:

'This will be ignored by Python'


On executing the above code we can see that there will not be any output so we utilize the strings with triple quotes(“””) as multiline comments.

Example 2: Multiline comments using string literals

""" Python program to demonstrate
 multiline comments"""
print("Multiline comments")


Output:-

Multiline comments


Python Docstring Comments

Python docstring is the string literals with triple quotes that are showed up just after the function. It is utilized to associate documentation that has been composed with Python modules, functions, classes, and methods. It is added right beneath the functions, modules, or classes to depict what they do. In Python, the docstring is then made accessible through the __doc__ attribute.

Example:

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


Previous                                                                                      Next