__str__-python

__str__ python method with examples in 2023

In this blog, we will learn everything about __str__ python function. We will understand the __str__ python function with multiple examples. so let’s get started.

what is __str__ function in python

__str__ Python

The __str__ method is a special method in Python classes that is used to define a string representation of an object. The method should return a string that provides a human-readable representation of the object’s state. This string is used when the object is printed, or when the built-in str() function is called on the object.

If the __str__ method is not defined for a class, the interpreter will use the default implementation, which returns the name of the object’s class and its memory address.

Syntax of __str__ python function

The syntax for the __str__ method in a Python class is as follows:

class MyClass:
    def __init__(self, ...):
        # Initialization code

    def __str__(self):
        return "string representation of the object"

The method should be defined within the class and should be named __str__. It should not take any arguments and it should return a string.

How to call a __str__ function in python

In Python, the __str__ method is called automatically when an object is used in a string context, such as when it is printed, or when the built-in str() function is called on the object.

For example, consider the following class My_person_class with an __str__ method defined:

class My_person_class:
    def __init__(self, name, city, age):
        self.name = name
        self.city = city
        self.age = age
    def __str__(self):
        return f"{self.name} who lives in {self.city} is {self.age} years old"

person1 = My_person_class("Tom","USA", 30)
print(person1)

The above code will create an instance of the class My_person_class called person1 with the name “Tom”, city “USA”, and age 30. And then when you print person1, the __str__ method will be called automatically and it will return a string representation of the object

__str__ python

Also, you can test it by calling str(person1) function, it will return the same string representation of the object.

print(str(person1))

__str__ python example

__str__ python-example

Below is an example of how the __str__ method can be used in a Python class.

class My_book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages
        
    def __str__(self):
        return f"Book {self.title} is written by {self.author} have {self.pages} pages"

book1 = My_book("Moby-Dick", "Herman Melville", 635)
print(book1)
print(str(book1))

If you run the above code, it will create an instance of the class My_book called book1 with the title “Moby-Dick”, author “Herman Melville”, and 635 pages. And then when you print book1, the __str__ method will be called automatically and it will return a string representation of the object:

__str__ python example

__str__ vs __repr__ function in python

In Python, the __str__ and __repr__ methods are used to define string representations of an object. The main difference between the two is their intended use and audience.

  • The __str__ method is used to define a string representation of an object that is intended for end-users.On the other hand, the __repr__ method is used to define a string representation of an object that is intended for developers.
  • _str__ return a string that provides a human-readable representation of the object’s state whereas __repr__ return a string that provides an unambiguous representation of the object’s state.
  • __str__ string is used when the object is printed, or when the built-in str() function is called on the object whereas __repr__ is used when the object is printed in the interpreter, or when the built-in repr() function is called on the object.
  • If the __str__ method is not defined for a class, the interpreter will fall back to using the __repr__ method and if the __repr__ method is also not defined, the interpreter will use the default implementation.
  • Below is an example of how the __str__ and __repr__ methods can be used in a Python class
class My_book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages
        
    def __str__(self):
        return f"Book {self.title} is written by {self.author} have {self.pages} pages"
        
    def __repr__(self):
        return f"Book('{self.title}', '{self.author}', {self.pages})"

book1 = My_book("Moby-Dick", "Herman Melville", 635)
print(book1)
print(str(book1))
print(repr(book1))

the above code, it will create an instance of the class My_book called book1 with the title “Moby-Dick”, author “Herman Melville”, and 635 pages. And then when you print book1, the __str__ method will be called automatically and it will return a string representation of the object

Book Moby-Dick is written by Herman Melville have 635 pages

And when you call str(book1) it will return the same string representation of the object as print(book1)

Book Moby-Dick is written by Herman Melville have 635 pages

And when you call repr(book1) it will return a string that provides an unambiguous representation of the object’s state, this string is used when the object is printed in the interpreter, or when the built-in repr() function is called on the object.

Book('Moby-Dick', 'Herman Melville', 635)

Conclusion

I hope you have liked this detailed tutorial on the __str__ python function. Please do let me know if you are facing any issues.

More to Explore

Python hashmap

Python NULL detailed tutorial

Python lowercase function

Python xor function

Python str function

Python __init__ method

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top