What is the __str__ method in Python?

Heads up — this lesson is optional bonus content. This lesson touches on classes, objects, and dunder methods, which haven't been formally introduced yet in the course. If any of that feels unfamiliar, don't worry — you can safely skip this lesson and come back to it later, once you've learned about classes. Nothing in the upcoming lessons or quiz depends on it. If you're curious and want a sneak peek at how Python represents objects as strings, read on!

The __str__ method represents a class's objects as a string. It should be defined so that the output is easy to read and includes the relevant members of the class, which also makes it useful as a debugging tool when you need to inspect an object's contents.

Python automatically calls __str__ when any of the following are used on an object:

  • print(obj)

  • str(obj)

  • obj.__str__()

If __str__ isn't defined on the class, Python falls back to __repr__ instead. By default, __repr__ returns a string describing the object's pointer (memory address), so without a custom __str__, printing your object will show something cryptic rather than a readable summary.

In the next sections, we'll look at two ways to use __str__: relying on the default behavior, and defining a custom version.

1. Default implementation

The following code explains the default implementation of the __str__ method.

Python 3.14.0
class MyClass:
x = 0
y = ""
def __init__(self, anyNumber, anyString):
self.x = anyNumber
self.y = anyString
myObject = MyClass(12345, "Hello")
print(myObject.__str__())
print(myObject.__repr__())
print(myObject)

The above code shows an example where neither __str__ nor __repr__ are defined. Calling __str__ calls the default __repr__ method, and they all give the same output, the pointer of our object.

2. Custom __str__method

The following code explains how the custom __str__ method works.

Python 3.14.0
class MyClass:
x = 0
y = ""
def __init__(self, anyNumber, anyString):
self.x = anyNumber
self.y = anyString
def __str__ (self):
return 'MyClass(x=' + str(self.x) + ' ,y=' + self.y + ')'
myObject = MyClass(12345, "Hello")
print(myObject.__str__())
print(myObject)
print(str(myObject))
print(myObject.__repr__())

The code above shows the output once you have defined the __str__ method. When __str__print(), or str() are called you will get your defined output. Make note that the __repr__ output remains the same.

3. The __repr__method defined only

Let’s see the example of the __repr__ method defined only.

Python 3.14.0
class MyClass:
x = 0
y = ""
def __init__(self, anyNumber, anyString):
self.x = anyNumber
self.y = anyString
def __repr__ (self):
return 'MyClass(x=' + str(self.x) + ' ,y=' + self.y + ')'
myObject = MyClass(12345, "Hello")
print(myObject.__str__())
print(myObject)
print(str(myObject))
print(myObject.__repr__())

In the first example we saw that when __str__ is not defined it automatically calls the __repr__ method. Therefore, the output of all the functions - __str__str(), and __repr__ - are the same. Moreover, the __repr__ method does not necessarily need to return a string. In case it does not return a string, the print() statements will throw an error.