gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:12:38 25 4
gpt4 key购买 nike

我是 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 .

关于python - python中的倒逗号和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3657792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com