Here is an example:

def __str__(self):
    return f'({self.self.country},{self.self.code},{self.self.product}, {self.cost},{self.quantity })'

This way, when you assign values to a class, you can print its string representation

print(new_shoe)

More info here https://www.pythontutorial.net/python-oop/python-str/

Answer from ssanga on Stack Overflow
🌐
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

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
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 - 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
string - __str__ method in Python - Stack Overflow
>>> class A: ... pass ... >>> a=A() >>> print a <__main__.A instance at 0x007CF9E0> If the class defines a __str__ method, Python will call it when you call str() or print: >>> class B: ... def __str__(self): ... return "I'm a B!" ... 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
The __str__ method in Python is a special method used to define a human-readable string representation of an object. This method is automatically invoked when an object is printed or converted to a string using the str() function.
🌐
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!
Find elsewhere
🌐
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)
🌐
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().
🌐
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 › 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.
🌐
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
Name: Alice Age: 30 Birth Year: 1993 String representation of the object: Alice, 30 years old Explicit string conversion: Alice, 30 years old ... We defined a __str__ method that returns a formatted string with the person's name and age.
🌐
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}'
🌐
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)
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))
🌐
Quora
quora.com › What-does-def-str__-self-method-does-in-Django
What does def __str__ (self) method does in Django? - Quora
Answer (1 of 2): It should be used to return a name for an object created using a particular model class. [code]class MyModel(models.Model): def __str__(self): return self.name name = models.CharField(max_length=10 [/code]