Python

Python __str__ Example

The __str__ function in Python is simply used to return the output of the Python functions in a string format. This function is mainly used with the user-defined functions since their outputs are generally not in the string format. For an easier understanding of those outputs, they must be converted into the string format. However, for the user-defined classes, you need to implement this function first. Moreover, another function known as the __repr__ function is always called whenever you call the __str__ function. Therefore, either you can define both these functions, or at least, you need to define the __repr__ function. You can read through this article completely to understand the usage of this function in Python.

Examples of __str__ in Python in Windows 10

Below are some of the different uses of the __str__ function of Python. By going through these examples thoroughly, you will understand how the functionality of this function differs between the default and user-defined classes.

Example 1

In this example, we wanted to use a pre-defined or default Python class, i.e., datetime, with the __str__ function. Here, we would like to state that for the pre-defined or default classes, all of these functions are implemented by default. You do not have to manually define them. The code that we wrote for demonstrating this example is as follows:

In this Python code, we first imported the default “datetime” class. After that, we declared a variable named “current” and assigned to it the value of the “datetime.now()” function of the “datetime” Python class. This function returns the current system date and time. After that, we wanted to print the value of the __str__ function followed by the value of the __repr__ function.

The output produced as a result of executing this Python script is shown in the image below:

We know that the __str__ and __repr__ functions were implemented by default since we were using a pre-defined class, i.e., datetime. The __str__ function simply returned the output of the “datetime.now()” function in the form of a string. In contrast, the __repr__ function returned the output of the “datetime.now()” function in a format using which this output can be reconstructed. The point to be noted here is that these results are merely based on the default implementations of the __str__ and __repr__ functions of Python.

Example 2

In this example, we wanted to use the __str__ function with a user-defined class in a way that both the __str__ and __repr__ functions were not defined. The code that we wrote for demonstrating this example is as follows:

In this Python code, we declared a class named “Employee”. Then, we initialized its attributes that were Emp_Name and Emp_ID. After that, we created an object of the “Employee” class, i.e., “E” and with the help of this object, we created an employee instance with the Emp_Name “Hasan” and Emp_ID “1”. Finally, we simply printed the value of the __str__ function followed by the value of the __repr__ function. We would like to mention again that we have not yet implemented any of these functions.

The output that was produced as a result of executing this Python script is shown in the image below:

Since we have dealt with a user-defined class in this example, we were supposed to implement the __str__ and __repr__ functions ourselves. However, we did not do so, which is why we simply received the address of the “Employee” object as our output.

Example 3

In this example, we wanted to use the __str__ function with a user-defined class so that only the __repr__ function will be defined and not the __str__ function. The code that we wrote for demonstrating this example is as follows:

In this Python code, we declared a class named “Employee”. Then, we initialized its attributes that were Emp_Name and Emp_ID. We have also defined the __repr__ function, and this definition is customized according to our own choice. After that, we created an object of the “Employee” class, i.e., “E” and with the help of this object, we created an employee instance with the Emp_Name “Hasan” and Emp_ID “1”. Finally, we simply printed the value of the __str__ function followed by the value of the __repr__ function.

The output that was produced as a result of executing this Python script is shown in the image below:

Since we have defined the __repr__ function in our code, the same definition was used to produce the output of both the __repr__ and the __str__ functions.

Example 4

In this example, we wanted to use the __str__ function with a user-defined class in a way that both the __repr__ and __str__ functions were defined. The code that we wrote for demonstrating this example is as follows:

In this Python code, we declared a class named “Employee”. Then, we initialized its attributes that were Emp_Name and Emp_ID. We have also defined the __repr__ and __str__ functions, and these definitions are customized according to our own choice. The __str__ function will display the output in the form of a complete statement, whereas the __repr__ function will simply return the string output values in our case. After that, we created an object of the “Employee” class, i.e., “E” and with the help of this object, we created an employee instance with the Emp_Name “Hasan” and Emp_ID “1”. Finally, we simply printed the value of the __str__ function followed by the value of the __repr__ function.

The output that was produced as a result of executing this Python script is shown in the image below:

Since both the __str__ and __repr__ functions were defined in our code and the definitions of both these functions differed from each other, the outputs of these two functions are different.

Example 5

In this example, we wanted to use the __str__ function with a user-defined class in a way that only the __str__ function will be defined and not the __repr__ function. The code that we wrote for demonstrating this example is as follows:

In this Python code, we declared a class named “Employee”. Then, we initialized its attributes that were Emp_Name and Emp_ID. We have also defined the __str__ function, and this definition is customized according to our own choice. After that, we created an object of the “Employee” class, “E”, and with the help of this object, we created an employee instance with the Emp_Name “Hasan” and Emp_ID “1”. Finally, we simply printed the value of the __str__ function followed by the value of the __repr__ function.

The output that was produced as a result of executing this Python script is shown in the image below:

Since we had only defined the __str__ function in our code, its value was printed correctly. Whereas for the __repr__ function, we again received the address of our “Employee” object since this function was not defined in our code.

Conclusion

In this guide, we first introduced you to the __str__ and __repr__ functions of Python. To understand the working of these functions well, we shared five different examples with you in which we showed you how the implementation of either or both of these functions affects the output of your Python code. The last thing that we would like to remind you of is if you use these functions with a default class, you will not need to define these functions explicitly. However, for the user-defined classes, it is mandatory to define these functions.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.