Short answer: Yes!


According to the Python docs (I highlighted the relevant part):

object.__str__(self)

Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object.

This method differs from object.__repr__() in that there is no expectation that __str__() return a valid Python expression: a more convenient or concise representation can be used.

The default implementation defined by the built-in type object calls object.__repr__().

So your_instance.__str__ is generally called when you do str(your_instance).


Longer answer: With "Special Methods" (the methods with two leading underscores and two trailing underscores) there is an exception because these are looked up on the class, not the instance. So str(a) is actually type(a).__str__(a) and not a.__str__(). But in most cases these are the same, because one rarely overrides methods of the class on the instance. Especially not special methods.

See also the relevant documentation on "Special method lookup":

For custom classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s type, not in the object’s instance dictionary.

So like @zzh1996 pointed out in the comments the following code will use the method defined on the class even though the instance has a custom callable __str__ attribute:

>>> class A(object):
...     def __str__(self):
...         return 'a'
>>> instance = A()
>>> instance.__str__ = lambda: 'b'
>>> str(instance)
'a'
>>> instance.__str__()
'b'
Answer from MSeifert on Stack Overflow
Top answer
1 of 3
29

Short answer: Yes!


According to the Python docs (I highlighted the relevant part):

object.__str__(self)

Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object.

This method differs from object.__repr__() in that there is no expectation that __str__() return a valid Python expression: a more convenient or concise representation can be used.

The default implementation defined by the built-in type object calls object.__repr__().

So your_instance.__str__ is generally called when you do str(your_instance).


Longer answer: With "Special Methods" (the methods with two leading underscores and two trailing underscores) there is an exception because these are looked up on the class, not the instance. So str(a) is actually type(a).__str__(a) and not a.__str__(). But in most cases these are the same, because one rarely overrides methods of the class on the instance. Especially not special methods.

See also the relevant documentation on "Special method lookup":

For custom classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s type, not in the object’s instance dictionary.

So like @zzh1996 pointed out in the comments the following code will use the method defined on the class even though the instance has a custom callable __str__ attribute:

>>> class A(object):
...     def __str__(self):
...         return 'a'
>>> instance = A()
>>> instance.__str__ = lambda: 'b'
>>> str(instance)
'a'
>>> instance.__str__()
'b'
2 of 3
3

Yes, it does. Take a look at the following example:

>>> class A:
...     def __init__(self, a):
...         self.a = a
...     def __str__(self):
...         print "Inside __str__"
...         return self.a
...
>>>
>>> a = A('obj1')
>>> a
<__main__.A instance at 0x0000000002605548>
>>>
>>> a.__str__()
Inside __str__
'obj1'
>>>
>>> str(a)
Inside __str__
'obj1'

And, from __str__() documentation, we have:

object.__str__(self)

Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object.

🌐
Educative
educative.io › answers › what-is-the-str-method-in-python
What is the __str__ method in Python?
This method is also used as a debugging tool when the members of a class need to be checked. The __str__ method is called when the following functions are invoked on the object and return a string:
Discussions

What does __str__ method do?
Yeah, it's just so you can print it. If you try to print a variable that is not a string it won't know what to do, so when you make a __str__ method it's like saying "If I tell you to print this variable, this is how I want you to do that" More on reddit.com
🌐 r/learnpython
13
4
November 14, 2020
python - How to use __str__ function to return a string representation of a class - Stack Overflow
I have been tasked with creating an inventory manager in python, and one of the required is to define the following function within the class: __str__ - This function returns a string representatio... More on stackoverflow.com
🌐 stackoverflow.com
python - Calling str method in classes - Stack Overflow
So i have class which construct i already have made and i need to make function for str print method. And i dont know how to call that str method in other functions of the same class Here is the More on stackoverflow.com
🌐 stackoverflow.com
In what cases do you use the str() function?
str() is used to generate a 'human readable string expression' for an object in python. Say for example , we have the following class in python: class Person: def __init__(self,name,age): self.name = name self.age = age print(Person('Anon',30)) This prints <__main__.Person object at 0x7f12b3fc7400> Which is a pointer to the memory address , not very helpful to comprehend. On the other hand , class Person: def __init__(self,name,age): self.name = name self.age = age def __str__(self): return f"Name:{name} Age:{age}" print(Person('Anon',30)) will print Name:Anon Age:30 which is more human readable ( kinda). The same thing happens with int - it has an inbuilt __str__ method which parses the input and converts it into a human readable string ( using ASCII/UTF-8 conversion or whatever). If you are still interested , I'd recommend going and reading up on __repr__ , which is similar to __str__ but is intended to be used via debugging rather than returning a string representation. Tbf , you can get around this using fstrings .... More on reddit.com
🌐 r/learnpython
8
8
July 16, 2021
🌐
Scaler
scaler.com › home › topics › what is python __str__?
What is Python __str__? | Scaler Topics
June 11, 2024 - 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. 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.
🌐
Vaia
vaia.com › all textbooks › computer science › starting out with python › chapter 10 › problem 11
Problem 11 How do you call the __str__ meth... [FREE SOLUTION] | Vaia
To do this, directly call the `__str__` method on the instance: ```python student_str = student1.__str__() print(student_str) ``` This will also output: ``` Student(name=Alice, age=18) ```
🌐
Finxter
blog.finxter.com › home › learn python blog › python __str__()
Python __str__() – Be on the Right Side of Change
December 6, 2021 - If you hadn’t defined the __str__() method, Python would’ve used the default implementation: Per default, any object has a __str__() method implementation—so you can represent any object x as a string explicitly by using the built-in str(x) function or using it implicitly by calling print(x).
🌐
Codecademy
codecademy.com › docs › python › dunder methods › __str__()
Python | Dunder Methods | __str__() | Codecademy
April 24, 2025 - The __str__() dunder method, also ... object. It can be called with the built-in str() and print() functions. Unlike __repr__(), it is not necessary that __str__() will return a valid Python expression.
🌐
AskPython
askpython.com › python › built-in-methods › python-str-function
Python str() Function - AskPython
July 12, 2022 - Let’s implement the __str__() function. def __str__(self): return f'Emp::ID={self.id}, Name={self.name}' ... The python str() function is one of the built-in functions. The str() function internally calls __str__() function, in absence it ...
Find elsewhere
🌐
Real Python
realpython.com › videos › how-and-when-use-str
How and When to Use .__str__() (Video) – Real Python
So, to ease into this with a simple example, I took the same Car class and I added a .__str__() method here. So, the dunder methods are methods that start with a double underscore (__)—it’s just kind of been shortened to dunder. 00:14 Some people refer to them as magic methods—a lot of people don’t like that because they’re not really supposed to be magical in any way. They’re just supposed to be, well, a Python convention.
Published: March 4, 2019
🌐
Geek Python
geekpython.in › str-and-repr-in-python
__str__ & __repr__: Change String Representation In Python
August 15, 2023 - The __str__() method returns the object’s easily-readable or informal string representation. Python calls the __str__() method internally when we call functions like str() and print().
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-str-repr-functions
How To Use the __str__() and __repr__() Methods in Python | DigitalOcean
Learn Python’s str() and repr() methods in depth. Explore examples, best practices, and real-world tips for clarity and debugging.
🌐
Reddit
reddit.com › r/learnpython › what does __str__ method do?
r/learnpython on Reddit: What does __str__ method do?
November 14, 2020 -

I'm still a beginner in python and my English is not really good, so I may have a little bit of understanding the explanation given in my course.

So I have this code:

class Apple:
    def __init__ (self,color,flavor):
        self.color=color
        self.flavor=flavor
        
    def __str__(self):
        return "This apple is {} and its flavor is {}".format(self.name,self.flavor)
        
jonagold=Apple("red","sweet")
print(jonagold)

The only purpose I see in using the __str__ method, I don't have to write the method in the print () line. If I changed the __str__ method with:

def say(self):
    return "This apple is {} and its flavor is {}".format(self.name,self.flavor)

I would just need to type

print(jonagold.say())

And I would get the same answer. What are the other purpose of using __str__? In what situation would I need to use the __str__?

Thank you :)

🌐
Leapcell
leapcell.io › blog › understanding-the-str-method-in-python
Understanding the `__str__` Method in Python | Leapcell
July 25, 2025 - The __str__ method is a special (or “magic”) method in Python used to define the “informal” or user-friendly string representation of an object. It is called when you use the built-in str() function or the print() function on an object.
🌐
dbader.org
dbader.org › blog › python-repr-vs-str
Python String Conversion 101: Why Every Class Needs a “repr” – dbader.org
August 8, 2017 - You should always add a __repr__ to your classes. The default implementation for __str__ just calls __repr__ internally, so by implementing repr support you’ll get the biggest benefit. On Python 2.x you’ll want to use __unicode__ instead of __str__.
🌐
LabEx
labex.io › tutorials › python-how-to-use-init-str-and-repr-methods-in-python-415189
How to use __init__, __str__, and __repr__ methods in Python | LabEx
When we call print(person1), Python automatically calls the __str__ method to determine what to display. We can also explicitly convert an object to a string using str(person1), which also calls the __str__ method.
🌐
Stack Overflow
stackoverflow.com › questions › 63466890 › calling-str-method-in-classes
python - Calling str method in classes - Stack Overflow
Now how i can call that str method in other functions. ... __str__ must return a string, not print it. Also, strings do not support a unary + – each use of + inside __str__ is an error. ... What have you tried so far to call the __str__ method? Do you know how to call other methods?
🌐
Stack Abuse
stackabuse.com › the-python-magic-methods-str-vs-repr
The Python Magic Methods: __str__ vs __repr__
August 27, 2023 - Magic methods can make your classes ... printable string representation of an object. This method is called by the str() built-in function and by the print function to convert the object into a string....
🌐
Medium
medium.com › @akshatgadodia › understanding-pythons-str-and-repr-methods-a-guide-for-python-developers-83490d83e4e9
Understanding Python’s __str__ and __repr__ Methods: A Guide for Python Developers | by Akshat Gadodia | Medium
August 14, 2026 - These are special methods in Python classes that are invoked to return string representations of objects. __str__: This method is called by the __str__() built-in function and the print statement to display a readable string representation of ...
🌐
Medium
medium.com › @btech.07 › str-method-add74b2760ae
__str__ method. The __str__ method in Python is a… | by Techie Ashu | Medium
December 4, 2024 - The __str__ method in Python is a special (or "magic") method that is used to define how an object should be represented as a string. It is called when you use str() on an object, or when you print an object using print().
🌐
Reddit
reddit.com › r/learnpython › in what cases do you use the str() function?
r/learnpython on Reddit: In what cases do you use the str() function?
July 16, 2021 -

Title. I know you use it for integers, but in what context? Also, is it used for anything else?