Python __str__ vs __repr__
Tags: python, programming, beginners
Published: 2023-05-31 13:52:14

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.

1. 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.
2. 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).
3. 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.
4. 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>
```

Learn more about [Python __str__ and __repr__](https://hackthedeveloper.com/python-__str__-vs-__repr__-method/) method from the original post.
[Find more blog about Python](https://www.google.com/search?q=python+__str__+site%3Ahackthedeveloper.com).