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

/*

*/

当前回答

是的,你可以简单地使用

'''
Multiline!
(?)
'''

or

"""
Hello
World!
"""

好处:它有点难,但在旧版本、打印函数或gui中使用更安全:

# This is also
# a multiline comment.

对于这个,你可以选择你想要注释的文本,并按Ctrl /(或⌘/),在PyCharm和VS Code中。

但是你可以编辑它们。例如,可以更改快捷键“Ctrl /” 按Ctrl Shift C。

警告!

小心,不要覆盖其他快捷方式! 注释必须正确缩进!

希望这个回答能有所帮助。祝你下次写出其他答案时好运!

其他回答

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

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

"""
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中的内嵌注释以哈希字符开始。

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.
'''

AFAIK, Python没有块注释。对于注释单个行,可以使用#字符。

如果你正在使用notepad++,有一个块注释的快捷方式。我相信其他的像gVim和Emacs也有类似的特性。

在Python 2.7中,多行注释是:

"""
This is a
multilline comment
"""

如果你在一个类中,你应该正确地制表。

例如:

class weather2():
   """
   def getStatus_code(self, url):
       world.url = url
       result = requests.get(url)
       return result.status_code
   """