Python repr() Function — A Helpful Guide with Example

Python’s built-in repr(obj) function returns the standard string representation of the provided object. This often includes the type and memory address of the object—for lack of further information. For example, the returned string representation may be '<main.Car object at 0x000001F66D11DBE0>' for a custom object of type Car. The function internally calls the method obj.__repr__() which is defined per default for all objects.

Here’s an example:

>>> class Car:
	pass

>>> repr(Car())
'<__main__.Car object at 0x000001F66D11DBE0>'
Python repr() Function Explanation

Syntax repr()

Syntax: 
repr(object)      # --> Returns standard (canonical) representation of an object. 
ArgumentsobjectObject for which the string representation should be returned.
Return ValuestringString representation of object.

Video repr()

Usage Examples repr()

The following code shows you how to use the repr(x) function on how to determine the string representation of some basic Python objects:

>>> repr(42)
'42'
>>> repr('42')
"'42'"
>>> repr([1, 2, 3])
'[1, 2, 3]'
>>> repr({'Alice': 'Bob', 'Bob': 'Alice'})
"{'Alice': 'Bob', 'Bob': 'Alice'}"
>>> repr(object)
"<class 'object'>"
>>> repr(repr)
'<built-in function repr>'

Here’s how you can define the string representation of your own custom objects:

class Car:
    def __repr__(self):
        return 'black tesla'


print(repr(Car()))
# black tesla

But before we move on, I’m excited to present you my new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners

How to Implement Your Own String Representation for a Custom Object

To implement your own string representation when using the repr(object) function on your custom object, overwrite the object.__repr__() method when defining the class and return the desired string representation of the given object. Note that if there’s a __str__() method defined, it takes precedence over the __repr__() method that is only used as a fallback that’s implemented per default for any object.

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))

The output is:

Your car has color black and brand porsche
Your car has color silver and brand tesla

Note how the __str__ method takes precedence over the __repr__ method. But if you skip the definition of the __str__ method, it’ll take the string returned by the __repr__ method:

class Car:
    def __init__(self, color, brand):
        self.color = color
        self.brand = brand

    def __repr__(self):
        return '123'

    
porsche = Car('black', 'porsche')
tesla = Car('silver', 'tesla')

print(str(porsche))
print(str(tesla))

The output now is:

123
123

Summary

Python’s built-in repr(obj) function returns the standard string representation of the provided object.

This often includes the type and memory address—for lack of further information about the object.

For example, the result may be '<main.Car object at 0x000001F66D11DBE0>' for a custom object of type Car.

>>> class Car:
	pass

>>> repr(Car())
'<__main__.Car object at 0x000001F66D11DBE0>'

The function internally calls the method obj.__repr__() which is defined per default for all objects.

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!