Python repr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

In Python, repr(obj) returns a string that describes obj for developers: strings get quotes and escape sequences, and many built-in values come back as text you could type into Python to recreate them, such as 'hello' or [1, 'a'].

It calls the object's __repr__() method. Containers use the repr of their items, the interactive prompt shows reprs, and str() falls back to __repr__() when a class has no __str__().

repr() is a built-in function: it lives in the builtins module, so it is always available without an import.

Syntax

Pass any object; you get its developer-facing text.

python
text = repr(obj)
text = f"{obj!r}"      # same thing inside an f-string
CallWhat it does
repr("hi")Returns "'hi'" with quotes.
repr("a\tb")Prints 'a\tb': the tab is shown as an escape.
repr(3.0)Returns "3.0".
f"{x=}"Prints the name and repr, such as x=5 (Python 3.8+).

Write __repr__() for every class you create; it makes lists of your objects and debugger output readable.

Library declarations
python
repr(object, /)
# calls type(object).__repr__(object)

Parameters

ParameterDescription
objectRequired. Any Python object.

Return Value

Return ValueDescription
strThe representation returned by __repr__().

Python Examples

Example 1 — Compare repr() and str() Side by Side

Print six values both ways in an aligned table, then show escapes.

python
values = ["hello", "it's", 42, 3.0, None, [1, "a"]]

for v in values:
    print(f"str: {str(v):<12} repr: {repr(v)}")
print(repr("tab\there\n"))

How It Works

  1. Quotes mark strings. str("hello") is the bare text, while repr() adds quotes so you know it is a string.
  2. Smart quoting. For "it's", repr() switches to double quotes to avoid escaping the apostrophe.
  3. Numbers and None. For 42, 3.0, and None both functions give the same text.
  4. Escapes visible. repr() shows the tab and newline as \t and \n instead of printing whitespace.

Example 2 — Write a Helpful __repr__ for Your Class

Give a class a repr that looks like the code that builds it, and use it in a list and in an f-string debug expression.

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

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


p = Point(2, -5)
print(repr(p))
print([p, Point(0, 0)])
print(f"{p=}")

How It Works

  1. Constructor-style text. __repr__ returns Point(2, -5), which reads like the call that created the object.
  2. !r inside. Using {self.x!r} keeps quotes if a coordinate is ever a string.
  3. Containers use repr. Printing a list calls repr() on each item, so both points are readable.
  4. Debug specifier. f"{p=}" (Python 3.8+) prints the expression, an equals sign, and the repr.

Example 3 — Default Reprs, Fallbacks, and Floats

Check the shape of the default repr without printing its address, and see that str() falls back to __repr__().

python
class Plain:
    pass


class Named:
    def __repr__(self):
        return "Named()"


text = repr(Plain())
print(text.startswith("<__main__.Plain object at"))
print(str(Named()), repr(Named()))
print(repr(1 / 3))
print(repr("caf\u00e9") == "'caf\u00e9'")

How It Works

  1. Default repr. Plain inherits object.__repr__, which starts with the module and class name and ends with an address; the example only checks the start.
  2. str() falls back. Named has no __str__, so str() uses its __repr__ as well.
  3. Shortest float. repr(1 / 3) prints the shortest text that reads back to exactly the same float.
  4. Printable Unicode kept. Unlike ascii(), repr() leaves é as it is.

Frequently Asked Questions

str() is for end users and returns readable text. repr() is for developers and returns an unambiguous form, with quotes around strings and escape sequences for special characters.
Return a string that looks like the constructor call, such as f"Point({self.x!r}, {self.y!r})". Use !r so nested values keep their own repr.
Your class uses the default object.__repr__. Define __repr__ (and optionally __str__) to show useful information.
It applies repr() to the value: f"{name!r}" is the same as repr(name). !s applies str() and !a applies ascii().
Only for many built-in types like numbers, strings, lists, and dicts. It is a convention, not a rule, and you should not eval text from untrusted sources.

Next: reversed()

Iterate over a sequence from the last item to the first.

reversed() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful