What is the difference between !r and %r in Python?

Question:

As the title states, what is the difference between these two flags? It seems they both convert the value to a string using repr()?
Also, in this line of code:

"{0!r:20}".format("Hello")  

What does the 0 in front of the !r do?

Asked By: b_pcakes

||

Answers:

%r is not a valid placeholder in the str.format() formatting operations; it only works in old-style % string formatting. It indeed converts the object to a representation through the repr() function.

In str.format(), !r is the equivalent, but this also means that you can now use all the format codes for a string. Normally str.format() will call the object.__format__() method on the object itself, but by using !r, repr(object).__format__() is used instead.

There are also the !s and (in Python 3) !a converters; these apply the str() and ascii() functions first.

The 0 in front indicates what argument to the str.format() method will be used to fill that slot; positional argument 0 is "Hello" in your case. You could use named arguments too, and pass in objects as keyword arguments:

"{greeting!r:20}".format(greeting="Hello")  

Unless you are using Python 2.6, you can omit this as slots without indices or names are automatically numbered; the first {} is 0, the second {} takes the second argument at index 1, etc.

Answered By: Martijn Pieters