Python __str__ vs __repr__
Tags: python, tutorial
Published: 2023-06-18 16:08:11

Have you ever thought about how you can make your custom Python objects more readable and easy to understand? Well, that’s where the Python __str__ and __repr__ methods come into the picture. It’s like a magical method that gives us the ability to decide how our objects should appear when we convert them into strings. In this blog, we’ll dive into the fascinating world of the __str__ and __repr__ methods and explore how they can elevate your programming skills.

## What is Python __str__ function?
The __str__ method in Python is a special function that lets us define how objects should be represented as strings. It provides a way for objects to introduce themselves in a human-readable format. When we print an object or use the __str()__ function, Python looks for the str method and calls it to get the string representation.

If the __str__ method is not explicitly defined for a class, the default implementation provided by the interpreter will be used. This default implementation returns a string containing the class name of the object and its memory address.

## Syntax of __str__ in Python
When it comes to understanding the syntax of the __str__ method in Python, we’ll break it down into simple steps. By following these steps, you’ll be able to implement the __str__ method effortlessly in your classes.

- Class Definition: To begin, we need to define our class. We use the class keyword followed by the name of our class. Inside the class, we define the __str__ method, which will determine how our object is represented as a string.
Method Definition: Within the class, we start defining the __str__ method. It’s important to note that the method name begins and ends with two underscores (underscores are like little huggers for the name).
- Return the String Representation: Inside the __str__ method, we need to specify what the method should return. This return value will be the string representation of our object. You can include any information you desire within this string representation, such as attribute values or custom messages.
- Complete the Syntax: Lastly, we need to make sure that we close our method definition and class definition properly. This is done by adding appropriate indentation and ensuring that everything lines up correctly.
Here’s an example to illustrate the syntax of __str__ in action:
```
class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed

    def __str__(self):
        return f"Meet {self.name}, the adorable {self.breed}."

my_dog = Dog("Max", "Labrador Retriever")
print(my_dog) # Meet Max, the adorable Labrador Retriever.
```
In this example, we define a class called Dog with attributes name and breed. The __str__ method is implemented to provide a string representation of a Dog object. When we print my_dog, the __str__ method is automatically called, and it returns a string with the dog’s name and breed.

If the __str__ method is not defined in the class and we attempt to print the object, the output would display the memory location of the class instead.
```
<__main__.Dog object at 0x7f5887f2d210>
```
## What is __repr__ function in Python?
In Python, alongside the well-known __str__ method, we have another special function called __repr__. While __str__ focuses on providing a human-readable string representation of an object, __repr__ takes a slightly different approach. It aims to provide an unambiguous and developer-oriented representation of the object.

By implementing the __repr__ method in our Python classes, we can define how the object should be represented when the repr() function is called or when the object itself is printed. This allows us to have fine-grained control over the output and enables other developers to understand the object’s structure and properties more effectively.

Read the full blog on: [Python __str__](https://hackthedeveloper.com/python-__str__-vs-__repr__-method/). Find the blog on Google: [Python __str__ __repr__](https://www.google.com/search?q=python+__str__+site%3Dhackthedeveloper.com).