double quotes in string representation

后端 未结 1 826
一整个雨季
一整个雨季 2020-12-19 01:30

This snippet:

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


        
相关标签:
1条回答
  • 2020-12-19 01:42

    Python is clever; 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: \'"'
    
    0 讨论(0)
提交回复
热议问题