Skip to main content
 首页 » 编程设计

python之python中的倒逗号和字符串

2025年05月04日21mate10pro

我是 python 的新手,但我已经写过很多程序,包括一些需要大量字符串操作的程序,例如下载管理器、游戏和文本编辑器。
为了表示字符串文字,我使用单引号或双引号。以当时我首先想到的为准。

虽然我还没有遇到任何麻烦。我的问题:python 允许两者的目的是什么,还是仅仅是为了兼容性、可用性等?

请您参考如下方法:

Python 中的 "string" 和 'string' 没有区别,所以,正如您所建议的,它只是为了可用性。

>>> 'string' == "string" 
True 

您还可以对多行​​字符串使用三重引号:

>>> mystring = """Hello 
... World!""" 
>>> mystring 
'Hello\nWorld!' 

另一个技巧是相邻的字符串文字会自动连接起来:

>>> mystring = "Hello" 'World!' 
>>> mystring 
'HelloWorld!' 

还有string prefixes which you can read about in the documentation .