In Python, the repr()
function is a valuable tool for obtaining printable representations of objects. This article aims to elucidate its usage, applications, and significance through comprehensive examples.
Understanding repr() Function
The repr()
function in Python is used to obtain a printable representation of an object. Its syntax is as follows:
Here, object
represents the Python object whose printable representation is to be obtained.
Example 1: Basic Usage
Output 1:
Example 2: Using with Strings
Output 2:
Example 3: Custom Objects
Output 3:
Points to Remember
- The
repr()
function returns a printable representation of an object. - It is often used for debugging and logging purposes to provide informative representations of objects.
- For custom objects, defining a
__repr__()
method allows customization of the printable representation.