repr()
repr(object) str · Updated March 13, 2026 · Built-in Functions The repr() function returns a string containing a printable representation of an object. This is useful for debugging, logging, and understanding what data you’re working with. For many types, the returned string is a valid Python expression that could recreate the object when passed to eval(). If that’s not possible, repr() returns a string enclosed in angle brackets describing the object.
Syntax
repr(object)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
object | object | — | Any Python object |
Returns: A string representation of the object.
Examples
Basic usage
The simplest way to see what an object looks like:
s = "hello world"
print(repr(s))
# 'hello world'
numbers = [1, 2, 3]
print(repr(numbers))
# [1, 2, 3]
data = {"name": "Alice", "age": 30}
print(repr(data))
# {'name': 'Alice', 'age': 30}
Notice how strings get quotes in the repr output, distinguishing them from other values.
With custom classes
When you define __repr__ on a class, repr() uses it:
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(3, 4)
print(repr(p))
# Point(3, 4)
Without __repr__, you’d get something like <__main__.Point object at 0x...>.
Distinguishing repr from str
Python calls __str__ when using print(), but __repr__ in other contexts:
from datetime import datetime
dt = datetime(2024, 1, 15, 9, 30)
print(str(dt))
# 2024-01-15 09:30:00
print(repr(dt))
# datetime.datetime(2024, 1, 15, 9, 30, 0)
Common Patterns
Debugging variables
The quickest way to inspect a variable during development:
value = some_complex_object
print(f"Debug: {repr(value)}")
Logging
Using repr for more informative log entries:
import logging
logger = logging.getLogger(__name__)
user_data = {"id": 42, "name": "Bob", "active": True}
logger.debug(f"User data: {repr(user_data)}")
Checking object identity vs equality
Useful for understanding when two variables point to the same object:
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(f"a == b: {a == b}") # True - same values
print(f"repr(a) == repr(b): {repr(a) == repr(b)}") # True
print(f"repr(a) == repr(c): {repr(a) == repr(c)}") # True
print(f"a is c: {a is c}") # True - same object
Creating eval-safe representations
For data that can be reconstructed:
import repr
# The repr module provides utilities for creating repr strings
data = {"key": [1, 2, 3], "nested": {"a": 1}}
print(repr.repr(data))
# {'key': [1, 2, 3], 'nested': {'a': 1}}
See Also
- str::str — returns a human-readable string representation (uses
__str__) - built-in::ascii — like repr() but escapes non-ASCII characters
- built-in::eval — can reconstruct an object from its repr (for simple types)