Double and single quotes using formatted printing in python

So I went through the "Learn Python Hard Way"

and wherein:

formatter = "%r %r %r %r"

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

there was a way out

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

But I'm not sure why the third line has double lines.

+4
source share
3 answers

"a"and 'a'- the same lines, no difference.

The third line contains an apostrophe, therefore it cannot be represented as 'But it didn't sing.', because it would end the line after didnand raise SyntaxError.

If you want to represent a string with a single quote, you can do this:

"'"

or

'\''

Same thing with a double quote:

'"'

or

"\""

If you have a string with both quotes, you can select one of them:

'"\'"

or

"\"'"
+6
source

'But it didn't sing' - - , , , ( " , ), - 1. 'But it didn' 2. 't sing', .

, , "" , , , , , , , , .. .

+2

', ". ' # 3, ' string '. , .

+1

Source: https://habr.com/ru/post/1622878/


All Articles