In Python, repr(obj) returns a string that describes obj for developers: strings get quotes and escape sequences, and many built-in values come back as text you could type into Python to recreate them, such as 'hello' or [1, 'a'].
It calls the object's __repr__() method. Containers use the repr of their items, the interactive prompt shows reprs, and str() falls back to __repr__() when a class has no __str__().
repr() is a built-in function: it lives in the builtins module, so it is always available without an import.
Syntax
Pass any object; you get its developer-facing text.
python
text = repr(obj)
text = f"{obj!r}" # same thing inside an f-string
Call
What it does
repr("hi")
Returns "'hi'" with quotes.
repr("a\tb")
Prints 'a\tb': the tab is shown as an escape.
repr(3.0)
Returns "3.0".
f"{x=}"
Prints the name and repr, such as x=5 (Python 3.8+).
Write __repr__() for every class you create; it makes lists of your objects and debugger output readable.
Constructor-style text.__repr__ returns Point(2, -5), which reads like the call that created the object.
!r inside. Using {self.x!r} keeps quotes if a coordinate is ever a string.
Containers use repr. Printing a list calls repr() on each item, so both points are readable.
Debug specifier.f"{p=}" (Python 3.8+) prints the expression, an equals sign, and the repr.
Example 3 — Default Reprs, Fallbacks, and Floats
Check the shape of the default repr without printing its address, and see that str() falls back to __repr__().
python
class Plain:
pass
class Named:
def __repr__(self):
return "Named()"
text = repr(Plain())
print(text.startswith("<__main__.Plain object at"))
print(str(Named()), repr(Named()))
print(repr(1 / 3))
print(repr("caf\u00e9") == "'caf\u00e9'")
Output
True
Named() Named()
0.3333333333333333
True
How It Works
Default repr.Plain inherits object.__repr__, which starts with the module and class name and ends with an address; the example only checks the start.
str() falls back.Named has no __str__, so str() uses its __repr__ as well.
Shortest float.repr(1 / 3) prints the shortest text that reads back to exactly the same float.
Printable Unicode kept. Unlike ascii(), repr() leaves é as it is.
Frequently Asked Questions
str() is for end users and returns readable text. repr() is for developers and returns an unambiguous form, with quotes around strings and escape sequences for special characters.
Return a string that looks like the constructor call, such as f"Point({self.x!r}, {self.y!r})". Use !r so nested values keep their own repr.
Your class uses the default object.__repr__. Define __repr__ (and optionally __str__) to show useful information.
It applies repr() to the value: f"{name!r}" is the same as repr(name). !s applies str() and !a applies ascii().
Only for many built-in types like numbers, strings, lists, and dicts. It is a convention, not a rule, and you should not eval text from untrusted sources.