What is Python __str__?

A unique method in Python called __str__ is used to define a string representation of an object that is readable by humans. This method, along with __init__, __repr__, and other "dunder" (double underscore) methods, is an element of the Python data model. Having a solid understanding of and proficiency with the __str__ method is essential to writing both functional and user-friendly classes.

__str__: What is it?

To obtain an object's string representation, the print() and str() functions invoke the __str__ method. Providing a "pretty" or human-readable representation of an object is the main objective of __str__; this is useful for logging and debugging.

Here is a simple example to show you how to use __str__:

Input:

Output:

Person(name=John, age=45)

In this example, Python internally uses the __str__ function to obtain a string representation of the person object when print(person) is invoked.

Input:

Output:

Employee name is John Doe, employee's age is 30 and id is 1024
Employee name is John Doe, employee's age is 30 and id is 1024
Employee name is John Doe, employee's age is 30 and id is 1024
Employee(first_name='John Doe', age=30, employee_id=1024)

The Python __str__ method is defined in this example, and the outcomes can be compared to those of the preceding example. In this case, the __str__ method is already defined, and the output obtained from calling the __str__, str(), and print() methods comes from the defined __str__ method. However, even though we have created the __str__ method, this is not the case with the __repr__ method, as you can see, as the output remains the same.

__repr__ Defined only

We will now only define the __repr__ method in this example. Let's see the result of defining only the __repr__ method and leaving the __str__ method undefined.

Input:

Output:

Employee(first_name=Roy Ridd, years_old=25, employee_id=2024)
Employee(first_name=Roy Ridd, years_old=25, employee_id=2024)
Employee(first_name=Roy Ridd, years_old=25, employee_id=2024)
Employee(first_name=Roy Ridd, years_old=25, employee_id=2024)

Differences Between __str__ and __repr__

Another comparable function in Python is named __repr__. Although string representations of objects can be returned using both __str__ and __repr__, their functions are distinct:

The goal of __str__ is to make it readable and easy to use. It is intended for end consumers or for the clear and concise display of information.

The purpose of __repr__ is to make things clear and practical for developers. It frequently offers more thorough details about the object that might be used to replicate it.

Here's an illustration to show the distinction:

Input:

Output:

Rosy, 38 years old
Person(name='Rosy', age=38)

Examples of __str__ in Python

Let's now examine a few samples to see how the __str__ method functions in various scenarios.

For instance:

The __str__ and __repr__ methods won't be specified in this example because we'll be using the __str__ method with a user-defined class. Let's look at the result of a case like this in the example that follows:

Input:

Output:

Student name is Dravid, number is 200, age is 27, and grade level is Senior
Student name is Dravid, number is 200, age is 27, and grade level is Senior
Student name is Dravid, number is 200, age is 27, and grade level is Senior

Python's __str__ function is an effective tool for producing comprehensible string representations of things. Using and customizing __str__ in your classes will greatly improve the readability and usability of your code, making it simpler to comprehend and debug for both you and other people. The __str__ method is a best practice that helps write cleaner, more manageable code, regardless of the complexity of the system you are designing.