76

According to the Python 2.7.12 documentation:

!s (apply str()) and !r (apply repr()) can be used to convert the value before it is formatted.

>>> import math
>>> print 'The value of PI is approximately {}.'.format(math.pi)
The value of PI is approximately 3.14159265359.
>>> print 'The value of PI is approximately {!r}.'.format(math.pi)
The value of PI is approximately 3.141592653589793.

Interestingly, the converted value is the output of repr(), rather than str().

>>> str(math.pi)
'3.14159265359'
>>> repr(math.pi)
'3.141592653589793'

So what does "convert the value" mean here? Making it less human-readable?

nalzok
  • 14,965
  • 21
  • 72
  • 139

3 Answers3

91

In order to format something in a string, a string representation of that something must first be created. "convert the value" is basically talking about how the string representation is to be constructed. In python, there are two fairly natural choices to get a string representation of something ... str and repr. str is generally a little more human friendly, repr is generally more precise. Perhaps the official documentation is the best place to go looking for the difference:

object.__repr__(self)

  • Called by the repr() built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned. The return value must be a string object. If a class defines __repr__() but not __str__(), then __repr__() is also used when an “informal” string representation of instances of that class is required.

  • This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.

object.__str__(self)

  • Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object.

  • This method differs from object.__repr__() in that there is no expectation that __str__() return a valid Python expression: a more convenient or concise representation can be used.

  • The default implementation defined by the built-in type object calls object.__repr__().

In str.format, !s chooses to use str to format the object whereas !r chooses repr to format the value.

The difference can easily be seen with strings (as repr for a string will include outer quotes).:

>>> 'foo {}'.format('bar')
'foo bar'
>>> 'foo {!r}'.format('bar')
"foo 'bar'"

What the difference between these two methods really depends critically on the objects being formatted. For many objects (e.g. those that don't override the __str__ method), there will be no difference in the formatted output.

Community
  • 1
  • 1
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • 8
    Explicit is better than implicit, use `repr` instead of `!r`. – mehmet Dec 09 '21 at 16:25
  • 18
    @mehmet. `!r` is pretty explicit too. It's like saying you should use `a = a + 1` instead of `a += 1` just because someone is unfamiliar with the syntax. – Mad Physicist Aug 17 '22 at 18:48
-1

If anyone, after reading those 2 answers here, is still wondering what to use !r or repr for, I'd like to shed some light on my own observation of our huge corporate shared library:

After scanning the usage of !r and repr in fstrings, I FOUND that 50% are with repr and 50% are with !r.

After looking at the blame on git, I found that people with !r contribution are somehow more passionate about programming than others with repr.

It is most likely that people who use repr() have not read the documentation of fstring (https://peps.python.org/pep-0498/#s-r-and-a-are-redundant), which allows !r as a short convenience. The repulsive word "redundant" from pep-049 gives the false impression that it is something to be avoided.

Andrei.Danciuc
  • 1,000
  • 10
  • 24
-3

I called the str(object) and the function -format() and print() to compute the as someone called it “informal”.

To perfectly print string representation of an object. The return value must be a string object as well.

Alg2code
  • 3
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 31 '21 at 00:29
  • This doesn't seem to answer the question of `!r` and `!s` in `str.format`. Also, please [edit] to provide details of what you mean by "informal". – Gino Mempin Oct 31 '21 at 05:55