Built-in function

repr()

Returns a string that shows a value the way you would type it in code, with quotes and escape codes visible. Ideal for debugging.

BeginnerEvery example below runs in your browser. Change the code and press Run.

repr(object)

repr() turns a value into a string meant for programmers. For a string it adds quotes and shows hidden characters such as new lines as \n, so you can see exactly what the value holds.

Compare it with str(), which makes the friendly version for users. str("hi") is hi, while repr("hi") is 'hi', quotes included. For most numbers both give the same text.

When you print a list, Python uses repr() for every item inside, which is why strings in a list appear with quotes. Your own classes can control their repr() by defining a __repr__ method; without one you get a vague text with a memory address.

Parameters

ParameterWhat it is
object
required
Any value: a string, number, list, your own object...

Return value

A string. For built-in types it usually looks like the code that would create the value.

Examples

1str() vs repr()

Pythoneditable
word = "hello"
print(str(word))
print(repr(word))
print(repr(42))
print(repr([1, "two", 3.0]))
Output
hello
'hello'
42
[1, 'two', 3.0]

2See hidden spaces and new lines

Data read from files or forms often hides a trailing space or a new line. repr() makes it visible.

Pythoneditable
name = "Anna \n"
print("Name:", name, "|")
print("Name:", repr(name))
print(repr("col1\tcol2"))
Output
Name: Anna
 |
Name: 'Anna \n'
'col1\tcol2'

The plain print breaks the line in the middle; repr() shows the \n and the space instead.

3Give your own class a useful repr

Pythoneditable
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return f"Point({self.x}, {self.y})"

p = Point(2, 3)
print(repr(p))
print([Point(0, 0), p])
Output
Point(2, 3)
[Point(0, 0), Point(2, 3)]

Lists print their items with repr(), so the list shows your custom text too.

4The !r shortcut in f-strings

Inside an f-string, !r calls repr() for you. It is the quickest way to debug a value.

Pythoneditable
city = " Madrid"
print(f"City is {city}")
print(f"City is {city!r}")
Output
City is  Madrid
City is ' Madrid'

Common mistakes

Showing repr() output to users

repr() is for developers. Messages for users should use the plain value or str(), otherwise they see quotes and escape codes.

Pythoneditable
item = "coffee"
print("You ordered " + repr(item))
print("You ordered " + item)
Output
You ordered 'coffee'
You ordered coffee

Forgetting __repr__ in your class

Without __repr__, printing your object gives text like <__main__.Point object at 0x...>, with an address that changes every run. Add a __repr__ method that returns something readable.

Pythoneditable
class Point:
    pass

text = repr(Point())
print(text.startswith("<__main__.Point object at 0x"))
Output
True

Questions people ask

What is the difference between __str__ and __repr__?

__str__ gives the friendly text used by print() and str(). __repr__ gives the exact, developer text used by repr(), the console and containers. If a class has only __repr__, Python uses it for both.

Can I turn repr() output back into the value?

For simple values like numbers, strings, lists and dicts, yes: ast.literal_eval(repr(x)) rebuilds them. See eval() for why literal_eval is the safe choice.

Why does printing a list show quotes around strings?

A list prints each item with repr(), so print(["a"]) shows ['a']. To print the items without quotes, use ", ".join(items).

Spotted a mistake on this page? Tell us