When we need to comment on multiple lines/statements, there are two ways to do this, either comment each line or create multiline comments (or block comment).
当我们需要对多个行/语句进行注释时,有两种方法可以执行此操作,或者注释每行或创建多行注释 (或阻止注释 )。
In C / C++, we use /*...*/ for the multiline comment, but in Python programming language, we have two ways to comment a section.
在C / C ++中 ,我们对多行注释使用/*...*/ ,但是在Python编程语言中,我们有两种方法来注释部分。
The first way is to comment on each line,
第一种方法是在每一行上注释,
This way can be considered as a single line comment in Python – we use the hash character (#) at the starting of each line to be commented.
这种方式可以视为Python中的单行注释–我们在要注释的每一行的开头使用井号( # )。
# This is line 1
# This is line 2
# This is line 3
And, the second way is to comment the section,
而且,第二种方法是评论该部分,
This way can be considered as a multiline comment in Python – we use triple single quotes (''') at the starting of the section to be commented and again triple single quotes (''') at the end of the second.
这种方式可以视为Python中的多行注释-我们在要注释的部分的开头使用三重单引号( ''' ),并在第二个末尾再次使用三重单引号( ''' )。
'''
This is line 1
This is line 2
This is line 3
'''
创建多行注释的Python示例 (A Python example for creating multiline comment)
'''
Function name: print_text
Parameters: None
Return type: None
Description: This function will print
some text on the screen
'''
def print_text():
print("Hello, world! How are you?")
if __name__ == "__main__":
# Here, we will call the print_text function
# that will print some text on the screen
print_text()
Output:
输出:
Hello, world! How are you?
In the above program, there are two multiline sections which we is commented,
在上面的程序中,有两个多行部分,我们在其中进行了注释,
Section 1:
'''
Function name: print_text
Parameters: None
Return type: None
Description: This function will print
some text on the screen
'''
Section 2:
# Here, we will call the print_text function
# that will print some text on the screen
翻译自: https://www.includehelp.com/python/how-do-we-create-multiline-comments-in-python.aspx