如何编写多行注释?大多数语言都有块注释符号,例如:
/*
*/
我建议不要使用 """
作为多行注释!
以下是一个简单的例子,以突显这可能被视为意外行为:
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
'clearly I'm 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
好的,您可以尝试这样做(在引用时,第一个问题的输入应该用'
引用):
"""
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中的多行注释:
对我来说,'''和"""都可以。
示例:
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)
print("Hello World") # printing
# single line comment
如果你需要编写超过一行的注释,可以使用三个引号
"""
This is a comment
written in
more than just one line
"""
"A sample single line comment "
多行文本:
"""
A sample
multiline comment
on PyCharm
"""
hello = "Hello!" # This is an inline comment
print(hello)
你好!
请注意,字符串字面值中的哈希符号只是一个哈希符号。
dial = "Dial #100 to make an emergency call."
print(dial)
hello = "Hello"
world = "World"
# First print hello
# And print world
print(hello)
print(world)
你好
世界
使用三个双引号将文本括起来以支持文档字符串。
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.
'''
是的,同时使用两者是可以的:
最初的回答
'''
Comments
'''
并且
"""
Comments
"""
然而,在IDE中运行时,您需要记住的唯一一件事是必须“运行”整个文件才能被接受为多行代码。逐行“运行”将无法正常工作并显示错误。
原始答案:最初的回答
(在Django模板中为{#,在Python脚本中为#)
进行注释或取消注释。{% 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.")
是的,您可以简单地使用
'''
Multiline!
(?)
'''
或者
"""
Hello
World!
"""
奖励: 这有点困难,但在旧版本中使用print
函数或GUI更加安全:
# This is also
# a multiline comment.
对于这个问题,你可以选择你想要评论的文本并按下 Ctrl
/
(或者 ⌘
/
),在 PyCharm 和 VS Code 中都适用。
但是你也可以编辑它们。例如,你可以将快捷键从Ctrl
/
更改为Ctrl
Shift
C
。
警告!
希望这个回答能有所帮助。祝你下次写回答好运!
这可以在Vim文本编辑器中完成。
移动到注释区域第一行的开头。
按下Ctrl+V进入可视模式。
使用箭头键选择要注释的所有行。
按下Shift+I。
按下#(或Shift+3)。
按下Esc。
#
作为注释的唯一方式是有道理的。我猜这样做可以让Python脚本更容易被解释。 - Victor Zamanian