How to Use the Repr() Function in Python

The Python repr() function is a very useful component of the language that can be used for a variety of purposes. This tutorial will dive deep into everything you need to know to use this built-in function effectively.

What is the Repr() Function?

The repr() function in Python is a built-in function used to return a string representing a printable version of an object. It helps us to have a better understanding of different types of objects and their values. The output of repr() could be used in debugging, logging, etc where complete information about the object is desired to be known.

How to Use the Repr() Function

To use the repr() function, just pass the object that you want the representation of as an argument to the function like this:

This will print out a string that shows a printable version of the object.

In-Depth Example of Repr() Function

Let’s consider a detailed example. Suppose we have two strings “Hello” and 5 and we want to use the repr() function on them. Here is how we can do it:

The output will be:

The output for the string is surrounded by quotes which shows that it’s a string, and the output for the number is not surrounded by quotes, indicating that it’s an integer.

Code Used in This Article

The complete code used in this article is as follows:

Conclusion

In conclusion, the repr() function is a powerful tool in Python that allows us to get a printable representation of an object. This can be very helpful for debugging, as it makes it easier to understand the structure and values of different objects.