I'm reading "Learn Python the hard way" (by Zed Shaw) and it says that %r
is used for "raw representation and debugging".
I'm sorry for such a uninformed question, but I have no idea what does it mean.
I'm reading "Learn Python the hard way" (by Zed Shaw) and it says that %r
is used for "raw representation and debugging".
I'm sorry for such a uninformed question, but I have no idea what does it mean.
So you know %s
is used for most strings, that just calls str()
over the variable that is formatted.
repr()
, on the other hand is used for %r
and will return the "raw string", which is easiest seen on string variables.
>>> print("%r vs. %s" % ('42','42'))
'42' vs. 42
Python printf String formatting
'r'
- String (converts any Python object usingrepr()
).
's'
- String (converts any Python object usingstr()
).
When it says it is useful for "debugging", imagine you had leading/trailing spaces in a string... the "raw" format would be more clear.
print("%r vs. %s" % (' hello',' hello'))
' hello' vs. hello