AlgoMaster Logo

__str__ & __repr__

Last Updated: January 3, 2026

5 min read

When you're working with Python objects, creating a clear and informative string representation can make a world of difference. Imagine you’re debugging your code or just trying to understand what an object holds.

If the output is cryptic or uninformative, it can feel like you're deciphering a puzzle without all the pieces. That's where the __str__ and __repr__ methods come into play.

They allow you to customize how your objects are represented as strings, making your life—and the lives of others who work with your code—much easier.

Understanding __str__ and __repr__

Both __str__ and __repr__ are special methods in Python that dictate how an object should be represented as a string. However, they serve different purposes, and understanding their distinctions is crucial for effective object-oriented programming.

__str__ vs __repr__

Purpose

  • __str__: This method is intended to provide a "pretty" or user-friendly string representation of an object. It's what gets called when you use print() on an object or when you call str().
  • __repr__: This method aims to provide a detailed and unambiguous string representation of an object, ideally one that could be used to recreate the object using eval(). It’s meant for developers and debugging, and is invoked by functions like repr().

When to Use Each

Use __str__ when you want a nice, easy-to-read output for end users. Use __repr__ when you need a more detailed representation aimed at developers.

Example

Let’s look at a simple example to illustrate the difference:

In this example, __str__ provides a readable description of the person, while __repr__ returns a detailed string that could help recreate the object.

Implementing __str__ and __repr__ in Your Classes

Let’s go deeper into how you can effectively implement these methods in your classes to enhance your debugging experience and clarity.

Customizing the Output

When you define these methods, you can customize them in various ways based on your class attributes. For instance, consider a Book class:

Real-World Use Cases

These representations are especially useful in real-world applications. For example:

  • Debugging: When inspecting objects in an interactive Python shell, having a clear __repr__ can save time. You can quickly see what attributes an object has without delving into the code.
  • Logging: When logging objects, you might want the __repr__ output for clarity, while user-facing outputs can utilize __str__.

Best Practices for __str__ and __repr__

Implementing these methods is straightforward, but there are a few best practices to keep in mind to maximize their utility.

Consistency and Clarity

  • Ensure that your __repr__ is unambiguous and could ideally recreate the object. Use the !r formatting option to get a string that is also valid Python syntax for the attributes.
  • Your __str__ should be concise but informative. Avoid cluttering the output with unnecessary details.

Example of Best Practices

Consider an Employee class:

In this example, both methods follow best practices. The __repr__ provides a clear and precise description, while __str__ gives a friendly, quick overview.

Handling Edge Cases

Sometimes, your objects may contain attributes that are themselves complex. Handling these cases properly in your string representations is essential.

Complex Attributes

If your object has nested objects or collections, you need to be cautious about how you represent them. Let's modify our Book example to include a list of reviews:

Potential Issues

You might run into issues with circular references or very large data structures. In such cases, ensure your __repr__ method is not overly verbose. You might want to truncate long lists or provide a summary instead.

Conclusion and Summary

In this chapter, we explored the __str__ and __repr__ methods, focusing on their differences, best practices, and real-world applications. We learned how to customize these methods in our classes to make our objects more user-friendly and developer-friendly. The proper use of these methods can significantly improve the usability and maintainability of your code.

Now that you understand how to create meaningful string representations with __str__ and __repr__, you’re ready to explore operator overloading.

In the next chapter, we will look at how to customize the behavior of our objects with operators, making them even more intuitive and powerful in your Python projects.