🌐
Educative
educative.io › answers › what-is-the-str-method-in-python
What is the __str__ method in Python?
The __str__ method in Python represents the class objects as a string – it can be used for classes. The __str__ method should be defined in a way that is easy to read and outputs all the members of the class.
🌐
Codecademy
codecademy.com › docs › python › dunder methods › __str__()
Python | Dunder Methods | __str__() | Codecademy
April 24, 2025 - The __str__() method is one of Python’s special methods, also known as dunder methods (double underscore methods). It allows defining how an object should be converted to a string when used with str() function or when displayed using print().
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 representation of a class. More on stackoverflow.com
🌐 stackoverflow.com
__str__() question
__str__() defines what happens when the object is cast to a string. Because print(p1) is converting p1 to a string, it automatically calls the __str__() method. On the other hand, print(my_new_car.get_descriptive_name()) is not converting my_new_car to a string; rather it's calling a method on my_new_car that returns a string. Essentially, print(SOME_OBJECT) is equivalent to print(SOME_OBJECT.__str__()) if __str__ is defined for that object type. More on reddit.com
🌐 r/learnpython
11
3
January 22, 2025
python - What is the purpose of __str__ and __repr__? - Stack Overflow
Use __str__ if you have a class, and you'll want an informative/informal output, whenever you use this object as part of string. E.g. you can define __str__ methods for Django models, which then gets rendered in the Django administration interface. More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
Real Python
realpython.com › videos › how-and-when-use-str
How and When to Use .__str__() (Video) – Real Python
In this lesson, you’ll see one Pythonic way to convert Python objects into strings by using .__str__(). You’ll learn what .__str__() means as well and also learn how to use it in a Python class: ... class Car: def __init__(self, color, mileage): self.color = color self.mileage = mileage def __str__(self): return 'a {self.color} car'.format(self=self)
Published: March 4, 2019
🌐
Manning
livebook.manning.com › wiki › categories › python › __str__
Understanding the __str__ Method in Python
Another example is a class representing RGB colors, where the __str__ method is used to return a string representation of the color’s red, green, and blue components: class Color: def __init__(self, red, green, blue): self._red = red self._green = green self._blue = blue def __str__(self): return f"Color: R={self._red:d},G={self._green:d},B={self._blue:d}"
🌐
Medium
hevalhazalkurt.medium.com › behind-the-underscores-ep03-string-representation-methods-str-repr-format-38f3a35948ca
Behind the Underscores EP03: String Representation Methods (__str__, __repr__, __format__) | by Heval Hazal Kurt | Medium
June 2, 2025 - Always return strings, not numbers or None. They have different jobs. Don’t make them twins unless your object is dead simple. def __str__(self): return str(self) # Infinite recursion!
🌐
W3Schools
w3schools.com › python › python_magic_str.asp
Python __str__() Method
The __str__() method is a magic method that controls what is returned when the object is printed, or passed to str().
🌐
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 :)

Find elsewhere
🌐
Medium
medium.com › @btech.07 › str-method-add74b2760ae
__str__ method. The __str__ method in Python is a… | by Techie Ashu | Medium
December 4, 2024 - This method returns a string that will be used to represent the object in a human-readable way. def __str__(self): return f"MyClass object is just to test"
🌐
Hostman
hostman.com › tutorials › how-to-use-the-str-and-repr-methods-in-python
How To Use the __str__() and __repr__() Methods in Python
This method is invoked when str() or print() functions are called on an object. ... class Person: def __init__(self, name, age): self.name = name self.age = age def __str__(self): return f"Person(name={self.name}, age={self.age})" person = Person("Alice", 30) print(person) # Output: Person(name=Alice, age=30)
🌐
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.
🌐
Scaler
scaler.com › home › topics › what is python __str__?
What is Python __str__? | Scaler Topics
June 11, 2024 - However, the python __str__ method prints a readable string representation, and the __repr__ method prints a message that is unambiguous. ... 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.
🌐
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
Use __str__ to provide a human-readable representation for end-users. Use __repr__ to provide a precise, unambiguous representation for developers and debugging. Let's put it all together by creating a Rectangle class with all three special methods: ... class Rectangle: def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height def perimeter(self): return 2 * (self.width + self.height) def __str__(self): return f"Rectangle with width {self.width} and height {self.height}" def __repr__(self): return f"Rectangle({self.width}, {self.
🌐
BelieveMy
believemy.com › python glossary › __str__
What is __str__ in Python? | Python glossary
February 9, 2026 - One of the most common questions in Python concerns the difference between __str__ and __repr__. Both methods serve to represent an object as text, but they have fundamentally different goals. Here is a concrete example showing this difference: ... class Product: def __init__(self, name, price): self.name = name self.price = price def __str__(self): return f"{self.name} — ${self.price}" def __repr__(self): return f"Product(name='{self.name}', price={self.price})" p = Product("Mechanical Keyboard", 89.99) print(str(p)) # Mechanical Keyboard — $89.99 print(repr(p)) # Product(name='Mechanical Keyboard', price=89.99)
🌐
AskPython
askpython.com › python › built-in-methods › python-str-function
Python str() Function - AskPython
July 12, 2022 - This output doesn’t help us because as a developer, there is no useful information about the object. Let’s implement the __str__() function. def __str__(self): return f'Emp::ID={self.id}, Name={self.name}'
🌐
Reddit
reddit.com › r/learnpython › __str__() question
r/learnpython on Reddit: __str__() question
January 22, 2025 -

I've been teaching myself Python using the Python Crash Course book, and now that I've reached the end of it I've been going through various tutorial websites to practice/supplement what I've learned. However, I've run into something strange.

As far as I can see, the Python Crash Course book does not mention __str__(). When I started looking at some class tutorials, as that's something I'm struggling with, I've run into examples where they use a method called __str__().

I'm shaky as to what it does, and I'm even more confused by the examples. I see one on https://www.w3schools.com/python/python_classes.asp which uses the following code:

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def __str__(self):
    return f"{self.name}({self.age})"

p1 = Person("John", 36)

print(p1)

When run, it returns John 36. Then there's another example where the __str__() method is not used, and it returns <__main__.Person object at 0x15039e602100>.

Okay, fine. So I need to use __str__() for this to work. But in the Python Crash Course, it shows an example with a similar class, and it does not use a __str__() method, and it works fine:

class Car:
    
    def __init__(self, make, model, year):
        self.make = make
        self.model = model 
        self. year = year
        
    def get_descriptive_name(self):
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()
    
my_new_car = Car("Audi", "a4", "2024")
print(my_new_car.get_descriptive_name())

This returns "2004 Audi A4". But it does not use a __str__() method. Why?

Top answer
1 of 8
92

__repr__

Called by the repr() built-in function and by string conversions (reverse quotes) to compute the "official" string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment).

__str__

Called by the str() built-in function and by the print statement to compute the "informal" string representation of an object.

Use __str__ if you have a class, and you'll want an informative/informal output, whenever you use this object as part of string. E.g. you can define __str__ methods for Django models, which then gets rendered in the Django administration interface. Instead of something like <Model object> you'll get like first and last name of a person, the name and date of an event, etc.


__repr__ and __str__ are similar, in fact sometimes equal (Example from BaseSet class in sets.py from the standard library):

def __repr__(self):
    """Return string representation of a set.

    This looks like 'Set([<list of elements>])'.
    """
    return self._repr()

# __str__ is the same as __repr__
__str__ = __repr__
2 of 8
71

The one place where you use them both a lot is in an interactive session. If you print an object then its __str__ method will get called, whereas if you just use an object by itself then its __repr__ is shown:

>>> from decimal import Decimal
>>> a = Decimal(1.25)
>>> print(a)
1.25                  <---- this is from __str__
>>> a
Decimal('1.25')       <---- this is from __repr__

The __str__ is intended to be as human-readable as possible, whereas the __repr__ should aim to be something that could be used to recreate the object, although it often won't be exactly how it was created, as in this case.

It's also not unusual for both __str__ and __repr__ to return the same value (certainly for built-in types).

🌐
Finxter
blog.finxter.com › home › learn python blog › python str() function
Python str() Function – Be on the Right Side of Change
February 14, 2021 - class Car: def __init__(self, color, brand): self.color = color self.brand = brand def __str__(self): return 'Your car has color ' + self.color + ' and brand ' + self.brand def __repr__(self): return '123' porsche = Car('black', 'porsche') tesla = Car('silver', 'tesla') print(str(porsche)) print(str(tesla))
🌐
Medium
fullstackdojo.medium.com › the-python-str-method-and-the-go-string-method-a-comparison-75770c78c4d8
The Python __str__ method and the Go String method: A comparison | by DSL | Medium
January 6, 2023 - In Python, you can customize the string representation of an object by defining the __str__ method in the object's class.