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

/*

*/

当前回答

我建议不要在多行注释中使用“”!

这里有一个简单的例子来强调可能被认为是意外的行为:

print('{}\n{}'.format(
    'I am a string',
    """
    Some people consider me a
    multi-line comment, but
    """
    'clearly I am also a string'
    )
)

现在看一下输出:

I am a string

    Some people consider me a
    multi-line comment, but
    clearly I am also a string

多行字符串不被视为注释,但它与'显然我也是一个字符串'连接起来,形成一个单一的字符串。

如果您想注释多行,请根据PEP 8指南执行:

print('{}\n{}'.format(
    'I am a string',
    # Some people consider me a
    # multi-line comment, but
    'clearly I am also a string'
    )
)

输出:

I am a string
clearly I am also a string

其他回答

我认为它没有,除了一个多行字符串没有被处理。然而,大多数(如果不是所有的话)Python ide都有一个短键来“注释掉”多行代码。

使用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.")

在windows中:你也可以选择文本或代码块,按CTR + /,如果你想删除注释,也可以这样做。 在mac中:它应该是comment + /

你可以使用下面的方法。这叫做DockString。

def my_function(arg1):
    """
    Summary line.
    Extended description of function.
    Parameters:
    arg1 (int): Description of arg1
    Returns:
    int: Description of return value
    """
    return arg1

print my_function.__doc__

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

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

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

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