Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

repr

repr(object) returns a printable string representation of an object. For many types it aims to produce text that can recreate an equal-valued object with eval(), while other objects use a descriptive fallback.

How it works

repr(object) produces a string intended to be a printable representation of the supplied object. For many built-in types, that string is designed so evaluating it recreates an object with the same value; other objects may use an angle-bracket description instead.

Syntax

repr(object, /)

Parameters

NameTypeDescription
objectobjectThe object whose printable representation is requested.

Returns

A string containing a printable representation of the object.

Key points

  • The result is always a string intended to show the object in a printable form.
  • For many types, Python aims for a representation that can recreate an equal-valued object.
  • The documented reconstruction convention uses eval() applied to the representation string.
  • Custom classes can define the representation behavior used by repr().

Common mistakes

  • Do not assume every result is executable Python: objects without a suitable value-oriented representation can produce an angle-bracket diagnostic string.
  • Fallback representations may include an object's memory address, so they can be unstable across runs and unsuitable for snapshots or durable serialization.

Example: Inspect a value representation

value = {"name": "Ada", "roles": ["admin", "developer"]}
text = repr(value)
print(text)
print(eval(text) == value)

Creates a printable representation of a dictionary, then demonstrates that this representation can be evaluated back to an equal value for this built-in type.

When to use it

Readable diagnostics

Investigating values during debugging

Round-trip inspection

Checking whether a built-in value has a reconstructible representation

Class-controlled representations

Implementing custom object diagnostics

Interview questions

QWhat does Python's repr() function return?

repr(object) returns a string containing a printable representation of the object. For many types, the representation is intended to produce an equal-valued object when passed to eval().

Follow-up: Can every repr() result be passed to eval()? No; the documentation explicitly describes angle-bracket fallback representations for cases where reconstruction is not available.

QWhat are the input and output of repr()?

The function accepts one object argument and returns a string. The result is representation text, not the original object itself.

Follow-up: How can a class influence this output? It can define a method controlling what repr() returns for its instances.

QHow can a class customize repr() for its instances?

A type can control the representation of its instances by defining a method. This lets custom objects provide their own printable representation.

Follow-up: What should a custom representation avoid? It should avoid implying reliable reconstruction unless the representation actually supports that use.

  • str
  • ascii
  • reprlib

Source: Built-in functions. Checked 2026-10-16.