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

/*

*/

当前回答

在其他答案中,我发现最简单的方法是使用IDE注释函数,该函数使用Python注释支持#。

我正在使用Anaconda Spyder,它有:

Ctrl + 1 -注释/取消注释 Ctrl + 4 -注释代码块 Ctrl + 5 -取消注释代码块

它可以用#注释/取消注释一行/多行代码。

我觉得这是最简单的。

例如,一个块注释:

# =============================================================================
#     Sample Commented code in spyder
#  Hello, World!
# =============================================================================

其他回答

这可以在Vim文本编辑器中完成。

转到评论区第一行的开头。

按“Ctrl+V”进入可视模式。

使用方向键选择要注释的所有行。

按下Shift +我。

按#(或Shift+3)。

按Esc。

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

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

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

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

在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__

Python中的内嵌注释以哈希字符开始。

hello = "Hello!" # This is an inline comment
print(hello)

你好!

注意,字符串文字中的哈希字符只是一个哈希字符。

dial = "Dial #100 to make an emergency call."
print(dial)

拨打100拨打紧急电话。

散列字符也可以用于单行或多行注释。

hello = "Hello"
world = "World"
# First print hello
# And print world
print(hello)
print(world)

你好 世界

用三双引号将文本括起来以支持docstring。

def say_hello(name):
    """
    This is docstring comment and
    it's support multi line.
    :param name it's your name
    :type name str
    """
    return "Hello " + name + '!'


print(say_hello("John"))

你好约翰!

在文本中加上三个单引号作为块注释。

'''
I don't care the parameters and
docstrings here.
'''