Python repr() Function Example Tutorial

Python repr() is an inbuilt function that returns a printable representation of a given object. The repr() method returns the printable representation of the given object. Here, obj is an object whose printable representation is to be printed.

Python repr()

It returns a string that would yield the object with the same value when passed to eval().

See the following syntax.

repr(obj)

Here, obj is an object whose printable representation is to be printed.

See the following example.

# app.py

data = 'foo'

print(repr(data))

Output

➜  pyt python3 app.py
'foo'
➜  pyt

See, we have got the same output as a string. It is a printable representation of a given object.

Working of repr()

See the following code.

# app.py

# Declaring two variables
name = "Appdividend"
msg = "Welcome to Appdividend"

# Now we will print those variable (objects)
# Using repr()
print(repr(name))
print(repr(msg))

Output

➜  pyt python3 app.py
'Appdividend'
'Welcome to Appdividend'
➜  pyt

Here, we assign a value ‘Appdividend’ to name and ‘Welcome to Appdividend’ to msg.

Python repr() method returns “‘Appdividend'”, ‘Appdividend’ inside double-quotes and “‘Welcome to Appdividend'”, ‘Welcome to Appdividend’ inside double-quotes.

We have assigned two strings to two variables name and msg. Now we have called the repr() method.

In the output field, we can see that output is in a single quote ( ‘ ‘ ). It is because name and msg are string types of variable, and repr() itself returns a string variable.

Implementation of repr() for custom objects

See the following program.

# app.py

class Student:
    name = "Debasis"

    # Defining repr of custom object
    def __repr__(self):
        return repr(self.name)


# Calling class object
print(repr(Student()))

Output

➜  pyt python3 app.py
'Debasis'
➜  pyt

In this example, we can see that we have created a class named Student and a class variable name. Then we have overridden the __repr__() method so that we can call repr() method outside the class quickly.

Python repr vs. str

Python str() and repr() both are used to get a string representation of an object.

Python str() is used for creating output for end-user while repr() is used primarily for debugging and development.

The repr() function’s goal is to be unambiguous, and str’s is to be readable. For example, if we suspect the float has a small rounding error, repr will show us while str may not.

The repr() compute the “official” string representation of the object (an illustration that has all information about an object), and str() is used to calculate the “informal” string representation of the object (a representation that is useful for printing an object).

The print statement and str() built-in function uses __str__ to display the string representation of an object while the repr() built-in function uses __repr__ to display the object.

Finally, Python repr() Function Example is over.

Related Posts

Python property()

Python open()

Python pow()

Python iter()

Python next()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.