当字符串将以双引号输出时?何时使用单引号?由%r输出

时间:2015-01-04 01:29:16

标签: python

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."
)

2 个答案:

答案 0 :(得分:4)

str.__repr__更喜欢单引号,除非字符串包含单引号且没有双引号,在这种情况下它使用双引号。

答案 1 :(得分:0)

只需使用format()功能,不关心%r,单引号,双引号,三引号。

formatter= "{} {} {} {}"
print ((formatter).format (
    "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.
    >>>