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
Parameter | Required/ Optional | Description |
---|---|---|
object | Required | A Python object. |
Returns
The function returns a string object.
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()))
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)
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.