Python Comments

Comments are used to explain the code.

Comments are written to make our code more readable for other programmers. The comment line is ignored by the Python interpreter.

To add comments to our program, we type a # sign in front of each line of comment:

#This is the comment
#This is comment 2
#This is the another comment

Example 1:

#This is the comment
print("Hello World!")

Output:

Hello World!

Example 2:

print("Hello World!") #This is the comment

Output:

Hello World!

Example 3:

print("Hello World!")
#print("Hello World!")

Output:

Hello World!

For multi-line comments, we can use # in each line or we can use three single quotes (or three double quotes), like this

Syntax 1 :

#This is the comment
#This is comment 2
#This is the another comment

Syntax 2 :

'''
This is the comment
This is comment 2
This is the another comment
'''

Example 1 :

#This is the comment
#This is the another comment
print("Hello World!")

Output :

Hello World!

Example 2: Multiline string line comment

'''
This is the comment
This is comment 2
This is the another comment
'''
print("Hello World!")

Output :

Hello World!

Example 3: Multiline string line comment

"""
This is the comment
This is comment 2
This is the another comment
"""
print("Hello World!")

Output :

Hello World!

You may also like...