Python 3 reference · Built-in Functions

Python repr() Function

Return the developer-friendly string representation of an object, often valid Python code that recreates the value.

Python 3 Since 3.0

Syntax

repr(object)

Parameters

ParameterDescription
object Any object.

Return value

A str.

Examples

repr() vs str()Try it yourself
text = "hi\nthere"
print(str(text))
print(repr(text))
print(repr(3.0), repr([1, "a"]))

Output

hi
there
'hi\nthere'
3.0 [1, 'a']
Define __repr__ for your classesTry it yourself
class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
    def __repr__(self):
        return f"Point(x={self.x}, y={self.y})"

print(repr(Point(1, 2)))
print([Point(0, 0)])  # containers use repr() for their items

Output

Point(x=1, y=2)
[Point(x=0, y=0)]
repr in f-stringsTry it yourself
name = "Ada"
print(f"{name!r}")  # !r uses repr() inside f-strings

Output

'Ada'

Good to know

str() is for end users; repr() is for developers (logs, debugging, the REPL). Give every class a __repr__(); lists and dicts show their items with it.

Version history

VersionChange
Python 3.0Added