Python __str__ method

In this Python tutorial, we will be reviewing the __str__ (str) method. This a special method that can be found in every Python Class. In this tutorial, we will be exploring how the __str__ method works, where it is used, and how to define our own __str__ method for Custom Classes.


What is the Python __str__ Function?

This is a special function that exists within every class, and it’s goal is to return a readable representation of a Class object. There is not much point in using them with pre-defined types like integers and strings, but they are used often with user-defined classes.

The reason for this is because if we try printing out a user-defined object, what do we get? Let’s take a look at a small example.

class Student:
    def __init__(self, name, age):
        self.age = age
        self.name = name

s = Student("John", 18)
print(s)
<__main__.Student object at 0x0000013745132DC8>

As you can see, it’s completely unreadable, and tells us next to nothing about the object and it’s values. So what’s the problem here? The problem is that the __str__ and __repr__ functions have not been defined yet. (We’ll get to the __repr__ function in a minute)


Defining the __str__ method

So let’s go ahead and define the __str__ method for our Custom student class.

class Student:
    def __init__(self, name, age):
        self.age = age
        self.name = name

    def __str__(self):
        return f"My name is {self.name} and my Age is {self.age}"

s = Student("John", 18)
print(s)
My name is John and my Age is 18

See? It’s much better now. It’s readable and it gives us information about the object.

You might have noticed that we didn’t actually use the __str__ function anywhere. The reason is that the print() function automatically calls the __str__ function if it has been defined. Otherwise we can manually call it as well, either by calling __str__ method or using the str function.

print(s)
print(str(s))
print(s.__str__())
My name is John and my Age is 18
My name is John and my Age is 18
My name is John and my Age is 18

As you can see, these 3 function calls are all equivalent.


The __repr__ function in Python

Understanding the repr function, or __repr__ method is essential for understanding __str__, as both of them are used in conjunction. Unlike __str__, whose goal is to produce a nice user-friendly version of the string, repr produces a code-friendly version.

class Student:
    def __init__(self, name, age):
        self.age = age
        self.name = name

    def __repr__(self):
        return f"Student({self.name}, {self.age})"

    def __str__(self):
        return f"My name is {self.name} and my Age is {self.age}"

s = Student("John", 18)
print(str(s))
print(repr(s))
My name is John and my Age is 18
Student(John, 18)

For more information on the Python repr function, be sure to check out our Python repr Function Tutorial!


This marks the end of the Python __str__ (str) method Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Leave a Comment