字符串文字以单引号开始,以三引号结束,它如何有效?

2024-04-26 00:35:02 发布

您现在位置:Python中文网/ 问答频道 /正文

问题1)。如果字符串以单引号开始,以三引号结束,它是如何有效的?你知道吗

问题2)。如果第二个打印语句有效,为什么第三个和第四个打印语句无效?你知道吗

print('This is "python" programming') #valid
print('This is "python" programming''') #valid
print(''This is "python" programming''') #Invalid
print('''This is "python" programming') #Invalid

Tags: 字符串is语句this引号programmingprintvalid
1条回答
网友
1楼 · 发布于 2024-04-26 00:35:02

在python中可以concatenate strings by putting them one next to other。你知道吗

例如:"a" "b",甚至在多行上:

re.compile("[A-Za-z_]"       # letter or underscore
           "[A-Za-z0-9_]*"   # letter, digit or underscore
          )

所以'This is "python" programming''''This is "python" programming'''的串联。你知道吗

三重引号('''""")是另一种在多行上具有字符串跨度的引号。你知道吗

来自python手册:

String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line.

第三个示例实际上是无效的,因为它是一个空字符串,后跟对python没有意义的标识符。你知道吗

最后一个示例无效,因为它以三重引号开始,但不关闭三重引号。你知道吗

相关问题 更多 >