__str__ returns a readable description of an object for end users, and __repr__ returns an unambiguous description for developers. print(obj), str(obj) and f-strings use __str__; the interactive prompt, repr(obj), debuggers, logs and the contents of containers use __repr__. If you implement only one, implement __repr__, because __str__ falls back to it.
The convention is that a good __repr__ looks like the code that would recreate the object, for example Person(name='Alice', age=30).
| Aspect | __str__ | __repr__ |
|---|---|---|
| Audience | End users | Developers |
| Goal | Readable | Unambiguous |
| Called by | str(), print(), format(), f-strings | repr(), REPL, debuggers, !r in f-strings |
| Used inside lists and dicts | No | Yes |
| Fallback if missing | Uses __repr__ | Default <module.Class object at 0x...> |
Both methods must return a str; returning anything else raises TypeError.
Without a __repr__, the object still prints well, but the REPL and lists show the default form, such as <__main__.Person object at 0x7f...>.
Notes on this version:
!r inside an f-string calls repr() on the value, so strings get quotes and escapes, which makes bugs visible.type(self).__name__ keeps the output correct for subclasses.__repr__ of each item, while print(people[0]) uses __str__.The standard library already follows this convention, which is a good way to remember it.
str(today) is the ISO date a user wants to read, while repr(today) is valid Python that rebuilds the object. For the string, repr adds quotes and shows the newline as an escape, so you can see exactly what the value contains. That is the quality to aim for in your own classes.
The fallback only runs one way: str() can use __repr__, but repr() never uses __str__. That is why __repr__ is the one to write first.
For plain data holders, @dataclass generates __repr__ for you (and __init__ and __eq__).
You can still add a hand-written __str__ for a friendlier display. dataclass(repr=False) turns the generated method off.
__repr__ informative and cheap to call. It runs in tracebacks and logs, so it must not raise exceptions or perform I/O.__repr__; logs often capture it.eval(repr(obj)) rebuild an equal object. If that is not realistic, use the angle-bracket style <Connection host='db' state='open'>, as the standard library does.__format__ if you need custom behaviour for format specifiers such as f"{obj:>10}".__str__ free of debugging detail; put that in __repr__.print() calls str() on its arguments, which uses __str__ and falls back to __repr__ when __str__ is missing.
It shows repr(obj). That is why a class without __repr__ appears as <__main__.ClassName object at 0x...>.
No. Write __repr__ for every class that is useful to debug, and add __str__ only when users need a different, nicer text.
Containers use repr() for their items so that the output is unambiguous, for example '1' and 1 are distinguishable.
__str__ is for people reading output, __repr__ is for developers reading logs and the REPL. Write __repr__ first, make it precise and safe, add __str__ for a friendlier format when needed, and use @dataclass when you only need a data holder.