簡體   English   中英

字符串表示中的雙引號

[英]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." 當其他三個項目都是單引號時,請加上雙引號?

(此代碼取自Learn Python the Hard Way Exercise 8。

Python很聰明; 在生成表示時,它將對包含單引號的字符串使用雙引號,以最小化轉義:

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

在那里加上一個雙引號,以及它會恢復到單引號和逃避包含的任何單引號:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM