What is !r called?

Viewed 297

I am seeing this for the first time. I wanted to know what the !r in the last line of the code called so that I can search about it. I found this piece of code on: https://adamj.eu/tech/2020/08/10/a-guide-to-python-lambda-functions/

class Puppy:
    def __init__(self, name, cuteness):
        self.name = name
        self.cuteness = cuteness

    def __repr__(self):
        return f"Puppy({self.name!r}, {self.cuteness!r})"
1 Answers

It's a format string conversion flag that tells the formatter to call repr on the object before formatting the string.

Three conversion flags are currently supported: '!s' which calls str() on the value, '!r' which calls repr() and '!a' which calls ascii().

Related