In Python format (f-string) strings, what does !r mean?

PythonPython 3.6F String

Python Problem Overview


I get what the new f strings in python 3.6 do, but what about the ending !r as found in the code snip below.

def __repr__(self):
    return (f'Pizza({self.radius!r}, 'f'{self.ingredients!r})')

Python Solutions


Solution 1 - Python

It just calls the repr of the value supplied.

It's usage is generally not really needed with f-strings since with them you can just do repr(self.radius) which is arguably more clear in its intent.

!r (repr), !s (str) and !a (ascii) were kept around just to ease compatibility with the str.format alternative, you don't need to use them with f-strings.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnk abramView Question on Stackoverflow
Solution 1 - PythonDimitris Fasarakis HilliardView Answer on Stackoverflow