Python repr()

Python repr() builtin function is used to get a string with printable representation of given object.

For many types, repr() function tries to return a string that would yield the same value when passed to eval() function.

In this tutorial, we will learn about the syntax of Python repr() function, and learn how to use this function with the help of examples.

Syntax

The syntax of repr() function is

repr(object)

where

ParameterRequired/OptionalDescription
objectRequiredA Python object.

Returns

The function returns a string object.

ADVERTISEMENT

Examples

1. Printable Representation of Class

In this example, we get the printable representation of a Python class and its object.

Python Program

class A:
    name = 'Apple'

print(repr(A))
print(repr(A()))
Try Online

Output

<class '__main__.A'>
<__main__.A object at 0x102fc6100>

2. Printable Representation of List

In this example, we get the printable representation of a list.

Python Program

x = ['hello', 25]
print(x)
Try Online

Output

['hello', 25]

Conclusion

In this Python Tutorial, we have learnt the syntax of Python repr() builtin function, and also learned how to use this function, with the help of Python example programs.