there are several ways to write string literals in python, with single or double quotes, single-line or multiline, raw or normal. None of that is preserved in the string itself, though; once the parser parses it, no information remains about how it was presented in the source; it doesn't even need to be in the source, you could generate the string dynamically, say, by reading it from a file, asking the user, or turning a number into a string.
So when you repr()
a string, python guesses which way to format it to look like a literal. The rules it uses are simple, if the string contains single quotes but no double quotes, it uses a single-line, double-quote, non-raw literal; In all other cases, it uses a single-line, single-quote, non-raw literal; Stated another way, python prefers single quotes, but if it's formatting a string that has single quotes, but not double quotes, it can repr()
that string without backslash escaping the quotes by using a double quoted string.
remember, repr()
doesn't return what you typed, because it doesn't know what you typed; you might never have typed it all. It returns something that can be parsed back into the same value. The identity* is:
x == eval(repr(x))
not
x == repr(eval(x))
* repr()
is not magical for this either, not all objects implement __repr__
in a way that preserves this constraint. repr is mostly used for providing useful debugging information, not for generating python code