๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_str.asp
Python str() Function
The str() function converts the specified value into a string. ... Coding fundamentals as bite-sized lessons and challenges. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: ...
๐ŸŒ
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().
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_strings.asp
Python Strings
Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range ... Magic Methods __str__ __repr__ __eq__ __add__ __len__ __lt__ __contains__ __call__ Code Challenge Python Inheritance
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_ref_string.asp
Python String Methods
Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range ... Magic Methods __str__ __repr__ __eq__ __add__ __len__ __lt__ __contains__ __call__ Code Challenge Python Inheritance
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Real Python
realpython.com โ€บ videos โ€บ how-and-when-use-str
How and When to Use .__str__() (Video) โ€“ Real Python
Okay. 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. Some peopleโ€ฆ
Published: March 4, 2019
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ trypython.asp
W3Schools online PYTHON editor
The W3Schools online code editor allows you to edit code and view the result in your browser
Find elsewhere
๐ŸŒ
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?

๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_format.asp
Python String format() Method
Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range ... Magic Methods __str__ __repr__ __eq__ __add__ __len__ __lt__ __contains__ __call__ Code Challenge Python Inheritance
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ dunder methods โ€บ __str__()
Python | Dunder Methods | __str__() | Codecademy
April 24, 2025 - Learn the basics of Python 3.13, one of the most powerful, versatile, and in-demand programming languages today. ... The __str__() method accepts no parameters other than the implicit self reference.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_strings_modify.asp
Python - Modify Strings
Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range ... Magic Methods __str__ __repr__ __eq__ __add__ __len__ __lt__ __contains__ __call__ Code Challenge Python Inheritance
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-str-function
Python str() function - GeeksforGeeks
May 1, 2025 - The str() function in Python is an in-built function that takes an object as input and returns its string representation. It can be used to convert various data types into strings, which can then be used for printing, concatenation, and formatting.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ built-in-methods โ€บ python-str-function
Python str() Function - AskPython
July 12, 2022 - When we call the str() function with an object argument, it calls the __str__() function of the object. So, we can implement the __str__() function for the object, which should return a string.
๐ŸŒ
Medium
medium.com โ€บ @btech.07 โ€บ str-method-add74b2760ae
__str__ method. The __str__ method in Python is aโ€ฆ | by Techie Ashu | Medium
December 4, 2024 - def __str__(self): return f"MyClass object is just to test" When you print an instance of MyClass, Python will use this method to get the string that represents the object.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_strings_format.asp
Python - Format Strings
Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range ... Magic Methods __str__ __repr__ __eq__ __add__ __len__ __lt__ __contains__ __call__ Code Challenge Python Inheritance
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ what is python __str__?
What is Python __str__? | Scaler Topics
June 11, 2024 - In Python, class objects are represented as strings with the help of the python __str__ method. This method is also called as magic method or dunder method. The python __str__ method returns the string representation of the object. The same method is also called when the str() or print() function ...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ str
Python str() (With Examples)
Online Python Online JavaScript Online SQL Online Java Online HTML Online C Online C++ Online C# Online PHP Online Swift Online Kotlin Online TypeScript Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... The str() method returns the string representation of a given object.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python-str-function.htm
Python str() Function
In Python, you can create a string by enclosing a sequence of characters within single quotes (' ') or double quotes (" ").