python 中的引号和字符串
我对 python 有点陌生,但我已经编写了许多程序,包括一些需要大量字符串操作的程序,例如下载管理器、游戏和文本编辑器。
为了表示字符串文字,我使用单引号或双引号......以当时首先想到的为准。
虽然我还没有遇到任何麻烦。我的问题:Python 允许两者都有什么目的,还是只是为了兼容性、可用性等?
I'm kinda' new to python, but I have already written many programs including some like download-managers, games and text-editors which require a lot of string manipulation.
For representing a string literal I use either single or double inverted commas.. whichever comes to my mind first at that time.
Although I haven't yet faced any trouble. My question: is there any purpose for which python allows both or is it just for compatibility, usability etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Python 中的
"string"
和'string'
之间没有区别,因此,正如您所建议的,这只是为了可用性。您还可以对多行字符串使用三引号:
另一个技巧是相邻的字符串文字会自动连接:
还有 您可以在文档中阅读的字符串前缀。
There is no difference between
"string"
and'string'
in Python, so, as you suggest, it's just for usability.You can also use triple quotes for multiline strings:
Another trick is that adjacent string literals are automatically concatenated:
There are also string prefixes which you can read about in the documentation.
这意味着您可以轻松地拥有一个带有任一单引号或双引号的字符串,而无需任何转义字符。所以你可以这样做:
It means you can easily have a string with either single or double quotes in it without needing any escape characters. So you can do:
只是为了兼容性和可用性。
当您将其中一个嵌入到字符串中时,有时它会很有用,因此我会使用
"Who's there?"
与'He said: "Hello"'
相比。另一个选项当然是三重引号字符串,就像
等于
Just for compatibility and usability.
It is sometimes useful when you have one of them embedded in the string, so I would use
"Who's there?"
compared to'He says: "Hello"'
.The other option is of course triple quoted strings, like
which is equal to