如何做多行注释?大多数语言都有块注释符号,比如:

/*

*/

当前回答

Visual Studio Code通用官方多行注释切换。类似于Xcode的快捷方式。

macOS:选择code-block,然后选择⌘+/

Windows:选择code-block,然后按Ctrl+/

其他回答

是的,两者都可以使用:

'''
Comments
'''

and

"""
Comments
"""

但是,在IDE中运行时,你需要记住的唯一一件事是,你必须“运行”整个文件,以接受多行代码。逐行“RUN”将无法正常工作,并将显示错误。

好吧,你可以试试这个(当运行引号时,第一个问题的输入应该用'引号括起来):

"""
print("What's your name? ")
myName = input()
print("It's nice to meet you " + myName)
print("Number of characters is ")
print(len(myName))
age = input("What's your age? ")
print("You will be " + str(int(age)+1) + " next year.")

"""
a = input()
print(a)
print(a*5)

"""之间的任何内容都将被注释。

如果你正在寻找单行注释,那么它是#。

如果在带有代码的一行中编写注释,则必须在#符号之前留下2个空格,在#符号之前留下1个空格

print("Hello World")  # printing

如果在新行上写注释,必须写注释,在#符号中留下1个空格kn

# single line comment

要编写超过一行的注释,需要使用3个引号

"""
This is a comment
written in
more than just one line
"""

你可以使用三引号字符串。当它们不是docstring(类/函数/模块中的第一件事)时,它们将被忽略。

'''
This is a multiline
comment.
'''

(确保适当缩进前导" '以避免出现IndentationError。)

Guido van Rossum (Python的创造者)在推特上发布了这条“专业提示”。

然而,Python的风格指南PEP8倾向于使用连续的单行注释,像这样:

# This is a multiline
# comment.

...这也是你在很多项目中会发现的。文本编辑器通常有一个快捷方式来轻松做到这一点。

Python中的多行注释:

对我来说,“’”和“”都管用。

例子:

a = 10
b = 20
c = a+b
'''
print ('hello')
'''
print ('Addition is: ', a+b)

例子:

a = 10
b = 20
c = a+b
"""
print('hello')
"""
print('Addition is: ', a+b)