What is Python __str__?
In Python, class objects are represented as strings with the help of the python __str__ method. This method is also called as magic method or dunder method. The python __str__ method returns the string representation of the object. The same method is also called when the str() or print() function is invoked on an object. We'll see examples of such instances in the article below.
Let's take a look at the syntax of the python str() method.
Need of Python __str__ Method
The python __str__ method returns the object representation in a string format. This method is supposed to return a human-readable format which is used to display some information about the object.
How to Call __str__ Method
Now, let’s see how you can use the python __str__ method. There are multiple ways in which you can call this method and these are explained below with examples.
Default Implementation
The output of the above python code snippet is shown below:
The above example shows the default implementation of the __str__ method. In the above example neither __str__ nor __repr__ methods are defined. Therefore, calling the __str__ method calls the default __repr__ method as explained above in the article. And as you can see in the output, all three ways give the same output.
Custom __str__ Method
Now, let's define our custom __str__ method only and see what would be the output.
The output of the above python code snippet that explains the custom __str__ method is shown below:
In this example, we have defined the python __str__ method and you can compare the results of this example from the previous example. Here, the __str__ method is defined already and when you call the __str__, str() and print() methods, the output we get is from the defined __str__ method. But, this is not the case with the __repr__ method as you can see the output of the __repr__ method is still the same although we have defined the __str__ method.
__repr__ Defined only
Now, in this example, we are going to define only the __repr__ method. Let's see what would be the output when the __str__ method is not defined and only the __repr__ method is defined.
The output of the above python code snippet is shown below:
__str__ Vs __repr__
The Python __str__ method returns the user readable string form of an object that can be understood by the end users. However, the __repr__ method in python also returns a string representation of an object which can be used for debugging purposes and development and it must be unambiguous.
You can see the __str__ as a method for end users and __repr__ as a method for developers.
Let's take an example to understand the difference between the __str__ and __repr__ methods in python.
Example:
In the above example, a cylinder class is created and an instance of the cylinder class is created namely cylinderOne. Here, when we print the object the __str__ method was called, and when the repr() function is used then it is called the __repr__ method, and similarly when the str() function is used then it called the __str__ method.
Here is the output of the above code snippet:
Output:
Examples of __str__ in Python
Now, let's take some examples to understand how the __str__ method behaves in different situations.
Example:
In this example, we'll use the __str__ method with a user-defined class in such a way that both the __str__ and __repr__ functions are not defined. Let's observe the output of such a case in the example below:
In the above code snippet, we have declared a class named Student. And some attributes were initialized. After that one object of the class Student is created namely studentOne. Finally, we've printed the value of the __str__ method. Note that we've not implemented the __str__ method in our code. Let's see what would be the output of the above code snippet.
Output:
As we've defined our class and an object is created on which we have used the python __str__ method and we were supposed to implement our own __str__ method to represent the object as a string. But we didn't do so that is the reason why we've received the address of the Student object as the output of the above code snippet.
Example:
Now, let's take a pre-defined python class, for example, a datetime class. As mentioned in the article above, for the default classes or pre-defined classes, the __str__ method is implemented by default and you don't have to manually define them. Let's see how will be the output of such a case in the example below:
In the above code snippet, a default datetime class is imported. After this, a variable named currentDateTime is created and it is assigned the value of the datetime.now() function of the pre-defined datetime python class. At last, the value of the __str__ method is printed. Let's take a look at the output of the above scenario.
Output:
As shown in the output above, the __str__ method returns the string representation of the object. Since we've used a default python class, the __str__ method returned the output of the datetime.now() function in the form of a string.
FAQs
Q: What is the difference between the str and repr method?
A: Both methods return the string representation of an object. However, the python __str__ method prints a readable string representation, and the __repr__ method prints a message that is unambiguous.
Q: What if str and repr methods are not defined?
A: By default when no __str__ and __repr__ methods are defined, the python __str__ method calls the default __repr__ method, and in both cases, these two will return the address of our object.
Learn More:
Conclusion
- In this article, we have discussed the __str__ and __repr__ methods with examples.
- The __str__ method is used to represent the string representation of an object that is human-readable.
- The __repr__ method is mainly used by developers to debug the python code snippets.
- If the class definition does not contain the __str__ method, then __repr__ method is invoked by python interpreter.
- When an object is passed to the str() function, the __str__ method in the class definition is invoked and this method returns the object's string representation.
- The __str__ method can be implemented in any class definition. But the only condition here is that the method should return a string value as discussed in the examples above.