问题: 解释如何在 Python 中注释多行,以便在代码中提供文档、解释或更长的注释。

解决方案: 在 Python 中,可以使用三重引号(''' 或"""). 这些被称为多行字符串字面量,通常用于注释目的,尤其是较大的注释块或文档。

下面是一个如何使用三重引号注释多行的示例:

'''
This is a multi-line comment.
You can write as many lines as you want here.
This is often used for documentation or longer comments.
'''

# Code execution continues here
print ("Hello, world!")

或者,您也可以用同样的方法使用三重双引号("""):

"""
This is another way to create a multi-line comment.
You can use triple double-quotes as well.
"""

# Code execution continues here
print ("Hello again, world!")

这些多行注释会被 Python 解释器忽略,不会影响代码的执行。它们通常用于记录代码,并为阅读代码的开发人员提供解释。对于仅供开发人员使用而非用于文档的代码注释,可以使用 # 符号来表示单行注释,或者使用三引号来表示多行注释,如上图所示。