Home > Article > Backend Development > How to create Python comments
Comments can be used to explain Python code.
Comments can be used to improve the readability of code.
When testing code, you can use comments to prevent execution.
Comments starting with #, Python will ignore them:
Example
#This is a commentprint("Hello, World!")
Run Example
Comments can be placed at the end of a line and Python will ignore the rest of the line:
Example
print("Hello, World!") #This is a comment
Run Example
The comment does not have to be text explaining the code, it can also be used to prevent Python from executing the code:
Example
#print("Hello, World!") print("Cheers, Mate!")
Running Example
Python actually has no syntax for multi-line comments.
To add multiple lines of comments, insert a # for each line:
Example
#This is a comment #written in #more than just one line print("Hello, World!")
Run Example
Alternatively, in a way that is not entirely expected, multiline strings can be used.
Since Python will ignore string literals that are not assigned to variables, you can add multi-line strings (triple quotes) to your code and add comments within them:
Example
""" This is a comment written in more than just one line """ print("Hello, World!")
Running Example
# As long as the string is not assigned to a variable, Python will read the code and then ignore it, and you're done Multiple lines of comments.
The above is the detailed content of How to create Python comments. For more information, please follow other related articles on the PHP Chinese website!