What is the difference between __str__ and __repr__?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, __str__ and __repr__ both produce string representations of an object, but they serve different audiences. __str__ is the user-facing, readable form, while __repr__ is the developer-facing, diagnostic form. In practice, you usually want __repr__ to be more precise and __str__ to be more friendly.
__repr__ Is for Developers
__repr__ is meant to help with debugging and inspection. Python uses it in places where an object needs an unambiguous representation, such as the interactive interpreter and repr(obj).
The common guideline is that __repr__ should, if practical, look like something useful to a programmer. Sometimes that means valid constructor-like code. Sometimes it just means a clear, detailed string with enough information to identify the object's state.
__str__ Is for Users
__str__ is used by str(obj) and by print(obj). Its job is usually to be readable rather than exhaustive.
The output from __str__ is what you would usually show in logs aimed at humans, console output, or UI text.
Fallback Behavior Matters
If you define __repr__ but not __str__, Python falls back to __repr__ when str() or print() is used.
That is why defining a good __repr__ is often the first priority. Even if you never add __str__, your objects will still display reasonably in many contexts.
Containers Usually Show repr, Not str
One subtle point surprises many developers: when Python prints a container such as a list or dictionary, it typically uses each element's repr, not its str.
That means a clean __str__ does not automatically make lists of your objects display cleanly. If container output matters, __repr__ is the more important method.
Use !r in f-Strings When Debugging
Python gives you an easy way to ask for repr formatting in f-strings.
This is especially useful when debugging strings with whitespace, escape characters, or values that are not obvious from the user-friendly display.
When to Define Both
Define both methods when the object has:
- a detailed internal state useful for debugging
- a simpler human-facing description useful for display
For tiny objects, one good __repr__ may be enough. For richer domain objects, having both often makes the class easier to work with.
Common Pitfalls
The first pitfall is making __repr__ too vague. A debug representation that only shows “object at address” is rarely useful.
Another issue is assuming print(list_of_objects) will use each object's __str__. In container displays, Python generally uses repr.
Developers also sometimes make __str__ detailed and __repr__ vague, which reverses their most useful roles.
Finally, do not obsess over making every __repr__ perfectly re-creatable by eval. That is a guideline, not an absolute rule. Clarity is more important than forcing literal reconstructibility in every class.
Summary
- '
__repr__is the developer-facing representation used for debugging and inspection.' - '
__str__is the user-facing representation used byprint()andstr().' - If
__str__is missing, Python falls back to__repr__. - Container output usually relies on
repr, notstr. - A strong
__repr__is often the most important representation method to implement first.

