Python Function Reference

repr

Get a string that shows the object the way it is written in code.

Python 3.13
repr(object)

Parameters

objectAny
Any object.

Return value

A string.

Examples

Comparison with str

Python 3.13
print(repr('hi'), str('hi'))
'hi' hi

A visible newline

Python 3.13
print(repr('line\n'))
'line\n'

Details

Strings are shown in quotes and special characters are escaped, which makes repr handy for debugging. Lists and dictionaries show their items with repr, hence the quotes in print(["a"]). For your own classes, define the __repr__ method; in an f-string, {value!r} gives the same form.

See also