Understanding the `__str__` Method in Python
James Reed
Infrastructure Engineer · Leapcell

Key Takeaways
- The
__str__
method defines a user-friendly string representation of Python objects. - Overriding
__str__
improves readability when printing or logging objects. __str__
is distinct from__repr__
, which targets developers and debugging.
In Python, every object has a string representation. The __str__
method plays a crucial role in defining how your objects are represented as strings. If you've ever printed an object or converted it to a string using str()
, you've interacted with __str__
—whether you knew it or not.
What Is __str__
?
The __str__
method is a special (or “magic”) method in Python used to define the “informal” or user-friendly string representation of an object. It is called when you use the built-in str()
function or the print()
function on an object.
Syntax
class MyClass: def __str__(self): return "This is a string representation of MyClass"
When you run:
obj = MyClass() print(obj)
Output:
This is a string representation of MyClass
Why Override __str__
?
By default, if you do not override __str__
, Python falls back to a generic implementation defined in the base object
class. That default typically returns a string like:
<__main__.MyClass object at 0x7f1a2b3c4d50>
Which is not very helpful to users or developers. Overriding __str__
allows you to make your class more readable, especially during debugging and logging.
Difference Between __str__
and __repr__
While both __str__
and __repr__
deal with string representations, they serve different purposes:
__str__
is meant to return a human-readable string.__repr__
is meant to return an unambiguous string that could be used to recreate the object, ideally.
Example
class Book: def __init__(self, title, author): self.title = title self.author = author def __str__(self): return f"{self.title} by {self.author}" def __repr__(self): return f"Book(title={self.title!r}, author={self.author!r})"
Now:
book = Book("1984", "George Orwell") print(str(book)) # 1984 by George Orwell print(repr(book)) # Book(title='1984', author='George Orwell')
Best Practices
- Keep your
__str__
output concise and user-friendly. - Only include the most relevant attributes that help describe the object.
- Don't overload the string with too much detail—leave that to
__repr__
.
When Is __str__
Called?
- When using
print(obj)
- When calling
str(obj)
- In f-strings like
f"{obj}"
If __str__
is not defined but __repr__
is, then str()
will fallback to __repr__
.
Conclusion
Overriding the __str__
method is a small but powerful way to make your Python classes more intuitive and user-friendly. By customizing how your objects are represented as strings, you can improve debugging, logging, and overall code clarity.
FAQs
It's called when using print()
, str()
, or f-strings like f"{obj}"
.
Python falls back to __repr__
if available; otherwise, it uses a default object string.
Yes, and it's a best practice to define both for clear output and debugging support.
We are Leapcell, your top choice for hosting Python projects.
Leapcell is the Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis:
Multi-Language Support
- Develop with Node.js, Python, Go, or Rust.
Deploy unlimited projects for free
- pay only for usage — no requests, no charges.
Unbeatable Cost Efficiency
- Pay-as-you-go with no idle charges.
- Example: $25 supports 6.94M requests at a 60ms average response time.
Streamlined Developer Experience
- Intuitive UI for effortless setup.
- Fully automated CI/CD pipelines and GitOps integration.
- Real-time metrics and logging for actionable insights.
Effortless Scalability and High Performance
- Auto-scaling to handle high concurrency with ease.
- Zero operational overhead — just focus on building.
Explore more in the Documentation!
Follow us on X: @LeapcellHQ