- Python Snacks
- Posts
- The difference between __repr__ and __str__
The difference between __repr__ and __str__
Understanding Python’s String Representations for Developers and Users

In Python, understanding the distinction between __repr__ and __str__ is crucial for effective debugging and user interaction.
Both are special methods that define how objects are represented as strings, but they serve different purposes.
__str__: The Informal String Representation
The __str__ method defines the “informal” or user-friendly string representation of an object.
It’s what gets displayed when you use print() or str() on an object. This representation is meant to be readable and informative for end-users.
Putting it into code:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f'Point at coordinates ({self.x}, {self.y})'
Then if we were to open an interactive Python session:
[user@host]$ python
>>> import Point
>>> p = Point(2, 3)
>>> print(p)
Point at coordinates (2, 3)
This provides a more descriptive and user-friendly output, suitable for displaying purposes.
__repr__: The Official String Representation
The __repr__ method aims to provide an unambiguous string representation of an object, primarily for developers.
It’s intended to be a precise depiction that, ideally, could be used to recreate the object. When you call repr() on an object or inspect it in an interactive session, Python invokes __repr__.
Putting it into code:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f'Point(x={self.x}, y={self.y})'
If we were to open a Python interactive session and call the __repr__ method:
[user@host]$ python
>>> import Point
>>> p = Point(2, 3)
>>> print(repr(p))
Point(x=2, y=3)
Here, the __repr__ is providing developers a clear and detailed representation of the Point object.
Key Differences Between __repr__ and __str__
Here’s the key differences between the two:
Purpose: __repr__ is for developers, offering an unambiguous representation, while __str__ is for end-users, providing a readable description.
Invocation:
__repr__ is called by the repr() function and in interactive sessions.
__str__ is called by the str() function and by print().
Fallback Mechanism: If __str__ is not defined, Python defaults to using __repr__ as the string representation.
Distinguishing between __repr__ and __str__ enhances both the developer’s debugging experience and the end-user’s interaction with your Python objects and should be implemented whenever possible.
Happy coding!
📧 Join the Python Snacks Newsletter! 🐍
Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.
This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.
Reply