Python Comments


Comments can be used to explain Python code.

Comments can be used to make the code more readable.

Comments can be used to prevent usage when checking code.


Creating a Comment

Comments start with #, and Python will ignore them:


Example
#This is a comment
print("Hello, World!")

Comments may be placed at the end of the line, and Python will ignore the remaining line:


Example
print("Hello, World!") #This is a comment

Comments do not have to be descriptive text, they can also be used to prevent Python from using code:


Example
#print("Hello, World!")
print("Cheers, Mate!")


Multi Line Comments

Python does not really have a syntax for multi-line comments.

To add multiple line comments you can add # to each line:


Example
#This is a comment
#written in
#more than just one line
print("Hello, World!")

Or, not as intended, you can use a multi-line cable.

Since Python will ignore virtual strings that are not shared in the dynamics, you can add a multi-string string (three quotes) to your code, and then place your comment inside:


Example
"""
This is a comment
written in
more than just one line
"
""
print("Hello, World!")

As long as the character unit is not assigned to a variable, Python will read the code, but then ignore it, and make multiple line comments.