__repr__ should return a printable representation of the object, most likely one of the ways possible to create this object. See also documentation for repr() (which calls __repr__). __repr__ is more for developers, while __str__ is for end users.

A simple example (interactive):

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __repr__(self):
        cls = self.__class__.__name__
        return f'{cls}(x={self.x!r}, y={self.y!r})'
p = Point(1, 2)
p

Output:

Point(x=1, y=2)
Answer from Miki Tebeka on Stack Overflow
🌐
Real Python
realpython.com › ref › builtin-functions › repr
repr() | Python’s Built-in Functions – Real Python
The .__repr__() method allows instances of the Book class to provide a detailed and developer-friendly string representation, which is useful for debugging and logging. ... Find out when to choose Python's __repr__() vs __str__() in your classes ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-repr-function
Python repr() Function - GeeksforGeeks
July 23, 2025 - Explanation: repr(object) function ... that the output is of type str. __repr__ method in custom classes defines how an object is represented as a string....
Discussions

A beginner's guide to __str__ and __repr__
You are the hero we both need and want. You get gold! More on reddit.com
🌐 r/learnpython
6
45
September 25, 2020
A beginner's guide to __str__ and __repr__ : r/learnpython
Subreddit for posting questions and asking for general advice about all topics related to learning python. More on reddit.com
🌐 r/learnpython
[Python] What's the difference between __str__ and __repr_?
__str__ is what gets used when you print() something or use str(...), and __repr__ is what gets used when you type that object's name at the interactive terminal, or use repr(...). For example, at the Python terminal, you can try this: >>> two_backslashes = r'\\' >>> print(two_backslashes) \\ >>> two_backslashes '\\\\' In this example, you stored two \ characters, and when printing that, exactly two characters appeared. But the "representation" of a string containing two backslashes consists of 6 characters -- an opening quote, followed by two pairs of doubled \'s, followed by a closing quote. In general, if you have created an object o, then the __repr__ member of that object should be defined such that the following creates a copy of o: o_copy = eval(repr(o)) After the copy is made, the following should be True if they represent the same contents and False otherwise: repr(o) == repr(o_copy) For example: class Person: def __init__(self, first, last): self.first = first self.last = last def __str__(self): return self.first + " " + self.last def __repr__(self): return "Person({}, {})".format(repr(self.first), repr(self.last)) If you create two person objects with different first and last names, they might appear to have the same contents based only on the string output: >>> author1 = Person("J. K.", "Rowling") >>> author2 = Person("J.", "K. Rowling") >>> str(author1) == str(author2) True But their 'representations' ought to be different: >>> repr(author1) == repr(author2) False >>> author1 Person('J. K.', 'Rowling') >>> author2 Person('J.', 'K. Rowling') As you can see the repr of the object gives you something that you can copy/paste (or eval) in order to recreate that exact same person. More on reddit.com
🌐 r/learnprogramming
4
1
October 11, 2019
repr function
The difference is the purpose of the string. str() creates a string for the user to see. repr() creates a string that represents the object as close as possible for debugging/info purpose. These two can sometimes be the same, but can sometimes be different. >>> import datetime >>> today = datetime.datetime.now() >>> print(str(today)) 2019-06-28 07:46:11.147169 >>> print(repr(today)) datetime.datetime(2019, 6, 28, 7, 46, 11, 147169) More on reddit.com
🌐 r/learnpython
20
37
June 28, 2019
🌐
Reddit
reddit.com › r/learnpython › a beginner's guide to __str__ and __repr__
r/learnpython on Reddit: A beginner's guide to __str__ and __repr__
September 25, 2020 -

Hi all, I made a couple of comments on another thread recently and I thought I'd pull them out into their own post for increased visibility. The intent here is to showcase some of Python's intermediate features in a beginner-friendly manner to get people thinking about how Python works under the hood. If you like this format please let me know and I can post more long-form answers in their own threads in the future.

Q: [<main.Test object at 0x123731860>, <main.Test object at 0x123731940>]

Why am I getting a weird output for print(c)?

Answer, part 1: You need to define a custom __repr__ function for your class to tell Python how to print it. By default it will just print the class and the memory location, which is the 'weird output' you're seeing here.

Full example:

class Test:
    def __init__(self, x, y, z):
        self.x, self.y, self.z = x, y, z
    
    def __repr__(self):
        return f"Test({self.x=}, {self.y=}, {self.z=})"

>>> a = Test(1, 2, 3)
>>> print(a)
Test(self.x=1, self.y=2, self.z=3)

Q:

How do lists print their strings?

Great question! To answer this in sufficient detail, we first need to discuss the difference between __str__ and __repr__.

__repr__, which I mentioned in my previous comment, is designed to be a full representation of an object - Essentially, by convention, it should contain everything required to disambiguate one object from another, and usually it's a string copy of the arguments used to construct the object in the first place.

__str__ on the other hand is the human-readable alternative, useful for very complex objects where we only want to display basic pieces of information for the purposes of logging or user interaction.

Now, when we do print(lst), the print function will make a call to the in-built function str - You'll probably recognize this function as we use it a lot to convert objects to strings! str(lst) will then delegate to lst.__str__(), which is our __str__ method.

The __str__ method of the list class looks (as a massive simplification) something like this - Note I've avoided using f-strings for clarity here, although they are the best-practice method for modern Python string formatting. (And yes, in reality the code below would be implemented in C, and it will use iter(self) to iterate over the items in the sequence)

def __str__(self):
    return '[' + ', '.join(repr(item) for item in self.items) + ']'

You'll see here that we, again, delegate the string representation of every item in the list down to item.__repr__. Why use __repr__ rather than __str__ for child items? It's to avoid including special characters such as newlines in the output and messing up the formatting - These are escaped as special characters in repr.

Finally, note that in my original answer, I define __repr__ but not __str__ and yet the code is still valid - Python is clever enough to fall back to __repr__ when __str__ is not defined, as the full __repr__ will always be more detailed than __str__.

References: Fluent Python, and this excellent SO post: https://stackoverflow.com/questions/1436703/difference-between-str-and-repr

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-__repr__-magic-method
Python __repr__() magic method - GeeksforGeeks
July 23, 2025 - Python __repr__() is one of the magic methods that returns a printable representation of an object in Python that can be customized or predefined, i.e.
🌐
Educative
educative.io › answers › what-is-the-repr-method-in-python
What is the __repr__ method in Python?
In Python, __repr__ is a special method used to represent a class’s objects as a string. __repr__ is called by the repr() built-in function.
🌐
Codecademy
codecademy.com › docs › python › built-in functions › repr()
Python | Built-in Functions | repr() | Codecademy
June 2, 2023 - The repr() function returns a printable string describing the object that is passed in. In other words, it returns the string representation passed implicitly to the eval() function.
Find elsewhere
🌐
Codecademy
codecademy.com › docs › python › dunder methods › __repr__()
Python | Dunder Methods | __repr__() | Codecademy
May 20, 2025 - In this example, the __repr__() method returns a string that represents the Person object in a way that could be used to recreate the object. When the object is printed or included in a collection, Python calls this method to obtain a string representation.
🌐
DEV Community
dev.to › jucheng925 › what-is-repr-in-python-and-how-does-it-compare-with-str-4lhf
What is __repr__ in Python and how does it compare with __str__? - DEV Community
February 16, 2024 - Based on the official python docs, repr() will "return a string containing a printable representation of an object. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed ...
🌐
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.
🌐
Scaler
scaler.com › home › topics › what is python __repr__?
What is Python __repr__? | Scaler Topics
May 4, 2023 - If one prints an instance or passes it to format as in str.format, if an \_\_str__ method has been defined, it will be called, otherwise, it will be \_\_repr__ that gets used. The \_\_repr__ function is called by the built-in repr function and is what is “echoed” onto one’s Python shell screen when evaluating an expression that returns an object.
🌐
BelieveMy
believemy.com › python glossary › __repr__
What is __repr__ in Python? | Python glossary
February 9, 2026 - Discover the __repr__ special method ... ... __repr__ is a special method (also called a dunder method or magic method) in Python that allows you to define the official representation of an object as a string....
🌐
DataFlair
data-flair.training › blogs › python-repr-function
Python repr Function With Example | repr vs str in Python - DataFlair
April 14, 2026 - To Python, this is a class that controls what it returns for its instances via the __repr__() magic method.
🌐
Quora
quora.com › What-is-repr-in-Python
What is ''__ repr __'' in Python? - Quora
Answer (1 of 7): __repr__ is a ‘magic’ method that can be implemented on a class in Python. The __repr__ method (normally called dunder_repr - double underscore repr) is used to return a string which should ideally be a representation of the relevant instance - i.e something that in theory ...
🌐
Initial Commit
initialcommit.com › blog › repr-in-python
__repr__ in Python - Initial Commit
When you use __repr__() or __str__(), the corresponding special method will run when the object instance is specified in the print() or repr() function. In this article, you looked at the special method __repr__() in Python and learned how it helps us in constructing objects.
🌐
Python Geeks
pythongeeks.org › python geeks › learn python › python repr() with examples
Python Repr() with Examples - Python Geeks
June 11, 2021 - It takes one object as its argument and returns a legal string representation of the passed object. ... Do remember that in Python, almost everything is an object.
🌐
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 - __repr__: This method is called by the __repr__() built-in function and by Python’s interactive interpreter to generate a representation of the object.
🌐
Hyperskill
hyperskill.org › university › python › repr-method-in-python-classes
__repr__ Method in Python Classes
August 2, 2024 - The ___ function, in Python is a pre installed feature that calculates the official string representation of an object. It's a function that gets automatically triggered when the repr() function is used on an object. The primary aim of the __repr__ function is to offer an clear string depiction ...
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › methods › built-in › repr › python-repr
Python repr() function | What is the use of Python repr() function? |
August 26, 2021 - In Python, __repr__ is a special method for representing the objects in a class as a string. The repr() built-in function calls __repr__. Using the __repr__ method, you can define your own string representation of your class objects.
🌐
Programiz
programiz.com › python-programming › methods › built-in › repr
Python repr()
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 repr() function returns a printable representation of the given object.