How to create Python comments

WBOY
Release: 2023-05-02 18:16:07
forward
1344 people have browsed it

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!")
Copy after login

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
Copy after login

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!")
Copy after login

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!")
Copy after login

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!")
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!