Using the repr() Function in Python

What is the repr() Function in Python

Python repr() is a built-in function in Python that returns a string representation of an object. The repr() function is short for “representation,” and it returns a string that represents the object passed to it in a way that can be evaluated by the Python interpreter. The string returned by repr() can be used to recreate the original object.

In this article, we will explore the Python repr() function in detail, including its syntax, usage, examples, and related concepts. The examples in this tutorial are written using Python 3.11.

Syntax

The syntax for the repr() function in Python is as follows:

repr(object)

Here, the object parameter is the object whose string representation is to be returned. The repr() function returns a string that represents the object in a way that can be evaluated by the Python interpreter.

Usage

The repr() function is used to obtain a string representation of an object in a way that can be evaluated by the Python interpreter. The string returned by repr() can be used to recreate the original object.

The repr() function is called implicitly by the Python interpreter when an object is printed using the print() function or when it is displayed in the interactive console. The string returned by repr() is also used when an object is passed to the eval() function.

Examples

Let’s explore some examples to understand the usage of the Python repr() function.

Example 1: Using repr() with Strings

s = 'Hello, World!'
repr(s)
# Output: "'Hello, World!'"

In the above example, we create a string variable s and pass it to the repr() function. The repr() function returns the string representation of the variable s.

Example 2: Using repr() with Integers

i = 42
repr(i)
# Output: '42'

In the above example, we create an integer variable i and pass it to the repr() function. The repr() function returns the string representation of the variable i.

Example 3: Using repr() with Lists

lst = [1, 2, 3, 4, 5]
repr(lst)
# Output: '[1, 2, 3, 4, 5]'

In the above example, we create a list variable lst and pass it to the repr() function. The repr() function returns the string representation of the variable lst.

Example 4: Using repr() with Tuples

tup = (1, 2, 3, 4, 5)
repr(tup)
# Output: '(1, 2, 3, 4, 5)'

In the above example, we create a tuple variable tup and pass it to the repr() function. The repr() function returns the string representation of the variable tup.

Example 5: Using repr() with Custom Objects

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __repr__(self):
        return f"Person('{self.name}', {self.age})"

p = Person('John', 30)
print(repr(p))
# Output: "Person('John', 30)"

In the above example, we create a custom object of the Person class and pass it to the repr() function. The Person class defines the __repr__() method, which returns a string representation of the object. The repr() function returns the string representation of the custom object.

str() vs repr()

In Python, there are two built-in functions for obtaining string representations of an object: str() and repr(). The str() function returns a string that represents the object in a way that is human-readable, while repr() returns a string that represents the object in a way that can be evaluated by the Python interpreter.

repr() Method

In Python, classes can define a special method called __repr__() that returns a string representation of the object. The repr() function implicitly calls the __repr__() method to obtain the string representation of an object. If possible, the __repr__() method should return a string that can be used to recreate the object when passed to eval().

If a class does not implement the __repr__() method, Python will use the default implementation, which returns a string in the format <classname object at memory_address>.

eval() Function

The eval() function in Python is used to evaluate a string as a Python expression. The string passed to eval() must be a valid Python expression. The string returned by repr() can be passed to eval() to recreate the original object.

Conclusion

Python repr() is a built-in function in Python that returns a string representation of an object. The string returned by repr() can be used to recreate the original object. The repr() function is called implicitly by the Python interpreter when an object is printed or displayed in the interactive console. The repr() function is useful for debugging and for creating string representations of objects that can be evaluated by the Python interpreter.