[英]double quotes in string representation
這個片段:
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."
)
運行時,打印此字符串:
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'
為什么"But it didn't sing."
當其他三個項目都是單引號時,請加上雙引號?
Python很聰明; 在生成表示時,它將對包含單引號的字符串使用雙引號,以最小化轉義:
>>> 'no quotes'
'no quotes'
>>> 'one quote: \''
"one quote: '"
在那里加上一個雙引號,以及它會恢復到單引號和逃避包含的任何單引號:
>>> 'two quotes: \'\"'
'two quotes: \'"'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.