简体   繁体   English

字符串表示中的双引号

[英]double quotes in string representation

This snippet: 这个片段:

formatter = "%r %r %r %r"
print formatter % (
    "I had this thing.",
    "That you could type up right.",
    "But it didn't sing.",
    "So I said goodnight."
)

when run, prints this string: 运行时,打印此字符串:

'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'

Why did "But it didn't sing." 为什么"But it didn't sing." get put in double quotes when the other three items were in single quotes? 当其他三个项目都是单引号时,请加上双引号?

(This code is taken from Learn Python the Hard Way Exercise 8 .) (此代码取自Learn Python the Hard Way Exercise 8。

Python is clever; Python很聪明; it'll use double quotes for strings that contain single quotes when generating the representation, to minimize escapes: 在生成表示时,它将对包含单引号的字符串使用双引号,以最小化转义:

>>> 'no quotes'
'no quotes'
>>> 'one quote: \''
"one quote: '"

Add a double quote in there as well and it'll revert back to single quotes and escape any single quotes contained: 在那里加上一个双引号,以及它会恢复到单引号和逃避包含的任何单引号:

>>> 'two quotes: \'\"'
'two quotes: \'"'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM