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

/*

*/

当前回答

选择要注释的行,然后使用Ctrl + ?在Sublime文本编辑器中注释或取消注释Python代码。

对于单行,可以使用Shift + #。

其他回答

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

print("Hello World")  # printing

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

# single line comment

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

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

如果你写了评论

"""
long comment here
"""

在脚本中间,Python/linters不会识别它。折叠将是混乱的,因为上面的注释不是标准建议的一部分。用起来更好

# Long comment
# here.

如果你使用Vim,你可以添加像评论这样的插件。通过按Vjgcc自动注释掉长行注释。其中Vj选择了两行代码,gcc将它们注释掉。

如果你不想使用像上面这样的插件,你可以使用搜索和替换

:.,.+1s/^/# /g

这将用#替换当前和下一行的第一个字符。

使用PyCharm IDE。

可以使用Ctrl+/对代码行进行注释和取消注释。 Ctrl+/用单行注释注释或取消注释当前行或几个选定的行(Django模板中的{#或Python脚本中的#)。 在Django模板中,对选中的源代码块按Ctrl+Shift+/,该代码块会被{% comment %}和{% endcomment %}标记包围。

n = 5
while n > 0:
    n -= 1
    if n == 2:
        break
    print(n)

print("Loop ended.")

选择所有行,然后按Ctrl + /


# n = 5
# while n > 0:
#     n -= 1
#     if n == 2:
#         break
#     print(n)

# print("Loop ended.")

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

"""
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)

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

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

Python 2.7.13:

单:

"A sample single line comment "

多行:

"""
A sample
multiline comment
on PyCharm
"""