How do I change the string representation of a Python class?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, the string representation of an object is controlled by special methods on the class. The important part is choosing the right method for the audience: people reading logs or terminal output usually want something different from developers debugging an object in a console.
Use __str__ for Readable Output and __repr__ for Debugging
Python gives you two main hooks:
- '
__str__for user-friendly text' - '
__repr__for an unambiguous developer-oriented representation'
When you call print(obj) or str(obj), Python uses __str__. When you call repr(obj) or inspect the object in many interactive environments, Python uses __repr__.
Typical output:
That split is a good default. The readable string is compact, while the debug representation exposes enough state to understand the object quickly.
Understand the Fallback Rules
If you do not define __str__, Python falls back to __repr__. If you define neither one, Python uses the inherited object representation, which looks like a class name plus a memory address.
The output is not very helpful because it does not describe the state of the object. That is why custom classes that appear in logs, exceptions, or APIs usually benefit from at least a custom __repr__.
A practical rule is:
- always define
__repr__for nontrivial classes - define
__str__when users need a cleaner display form
Keep __repr__ Informative and Safe
A strong __repr__ often looks like constructor-style output, but it does not have to be perfectly evaluable. The more important goal is clarity.
The !r formatter matters because it uses each field's own repr, which adds quotes around strings and makes ambiguous values easier to distinguish.
You should also think about sensitive data. If a class contains secrets, tokens, or passwords, exclude them from __repr__ so they do not leak into logs.
Dataclasses and Formatting Hooks
If you use @dataclass, Python will generate a useful __repr__ automatically, which is often enough for small value objects.
For more specialized display logic, you can also implement __format__, which affects formatted strings such as f"{obj:short}". Most classes do not need this, but it is handy when you want multiple display modes.
Common Pitfalls
- Putting too much text in
__str__. A string representation should be concise enough for logs and debugging output. - Using
__str__only and skipping__repr__. Developer tools often rely onrepr, so that is usually the more important method. - Returning non-string values. Both methods must return a string, or Python raises
TypeError. - Exposing secrets in object output. Debug output often reaches logs, test failures, and monitoring systems.
Summary
- Implement
__str__for readable output shown to users. - Implement
__repr__for detailed developer-facing output. - If
__str__is missing, Python falls back to__repr__. - Use
!rinside__repr__to make field values clearer. - Prefer concise, informative representations and avoid leaking sensitive state.

