Discuss / Python / 疑问

疑问

Topic source

L.W.X.

#1 Created at ... [Delete] [Delete and Lock User]

请问如果要打印
‘hello,

world’

这样写为什么运行不了?

print(r''''hello,

world'''')

LAS

#2 Created at ... [Delete] [Delete and Lock User]

print('hello\nworld') 这样即可

L.W.X.

#3 Created at ... [Delete] [Delete and Lock User]

我是指单引号需要转义,而且需要多行打印的情况下输出:

‘hello,

world’

如果用r''' '''的方式,前后各多加一个单引号的情况下,后面的四个单引号会报错

廖雪峰

#4 Created at ... [Delete] [Delete and Lock User]
print('\'hello,\nworld\'')

烟Mu

#5 Created at ... [Delete] [Delete and Lock User]

print('''\'Hello,

world\'''')

print(r"""'hello,
world'""")

这样就可以了,如果你要print的内容里包含单引号,那么你r后面就跟双引号...反之亦然

#7 Created at ... [Delete] [Delete and Lock User]

print("\'Hello,\n world\'")

用双引号就可以了

print("'hello,\nworld'")

这样也行

Catlair

#9 Created at ... [Delete] [Delete and Lock User]

很明显的语法歧义。你''''到底是' '''还是''' '。python表示看不懂,你写成"""'就可以了

这种就不用 r""了,直接写成 print("\'hello,\n\nworld\'")就行,用r反而不方便


  • 1
  • 2

Reply