Home  >  Article  >  Backend Development  >  How to create Python comments

How to create Python comments

WBOY
WBOYforward
2023-05-02 18:16:071135browse

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.

Create Comments

Comments starting with #, Python will ignore them:

Example

#This is a commentprint("Hello, World!")

Run Example

How to create Python comments

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

How to create Python comments

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

How to create Python comments

#Multi-line comments

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

How to create Python comments

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

How to create Python comments

# 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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete