Python is a powerful and versatile programming language, and one of its key strengths is the ability to represent objects in different ways. The repr() function in Python plays a crucial role in how objects are displayed and understood by developers. But what exactly is repr()? Why do we need it? And how can we use it effectively? In this detailed guide, we’ll explore everything you need to know about repr() in Python, including its differences from str(), how it works with custom objects, and when you should use it. By the end of this guide, even a beginner will have a solid understanding of repr().
What Is repr() in Python?
The repr() function in Python is used to return a string representation of an object. This string is usually meant for debugging and development rather than user-facing output. In simple terms, repr() provides a more detailed or unambiguous version of an object’s representation.
For example:
python
CopyEdit
x = 10
print(repr(x))
Output:
bash
CopyEdit
’10’
While this example seems simple, repr() is especially useful when dealing with more complex objects like lists, dictionaries, or custom classes. It helps developers understand how an object is structured and how it behaves in Python.
Why Do We Need repr()?
In Python, we often need to convert objects into strings for debugging or logging purposes. While the str() function is used to create human-readable representations, repr() is designed to generate unambiguous and often more detailed outputs.
Here are some reasons why repr() is useful:
- It helps developers see an object’s structure clearly.
- It provides a precise and unambiguous string representation.
- It is useful in debugging and testing, as it shows object details that str() might hide.
- It can be customized for custom classes to display important object information.
Using repr(), you can print objects in a way that helps developers understand their exact structure, which is particularly useful when debugging large projects.
How to Use repr() in Python
Using repr() in Python is simple. You can call the repr() function and pass any object as an argument.

Basic Usage of repr()
python
CopyEdit
num = 42
print(repr(num))
Output:
bash
CopyEdit
’42’
It works on different types of objects, how to use repr in python strings, lists, and dictionaries.
python
CopyEdit
text = “Hello, World!”
print(repr(text))
Output:
arduino
CopyEdit
“‘Hello, World!'”
Notice how the output includes extra quotes to show that it’s a string.
How Is repr() Different from str()?
The main difference between repr() and str() is their intended purpose:
- repr() is designed to produce an unambiguous representation of an object, typically useful for debugging.
- str() produces a human-readable representation that is meant to be more user-friendly.
Example:
python
CopyEdit
text = “Hello, World!”
print(str(text))
print(repr(text))
Output:
arduino
CopyEdit
Hello, World!
“‘Hello, World!'”
While str() simply prints the string, repr() includes additional details like quotation marks. This difference is useful when working with debugging logs or serialized objects.
Using repr() with Custom Objects
In Python, you can define custom behavior for repr() in your own classes using the __repr__ method.
Here’s an example:
python
CopyEdit
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return f”Person(name='{self.name}’, age={self.age})”
p = Person(“Alice”, 30)
print(repr(p))
Output:
scss
CopyEdit
Person(name=’Alice’, age=30)
By defining the __repr__ method, you control how Python represents instances of your class, making debugging easier.
Example: Custom Class with repr()
Let’s take another example with a more complex class:
python
CopyEdit
class Car:
def __init__(self, brand, model, year):
self.brand = brand
self.model = model
self.year = year
def __repr__(self):
return f”Car(brand='{self.brand}’, model='{self.model}’, year={self.year})”
my_car = Car(“Toyota”, “Corolla”, 2022)
print(repr(my_car))
Output:
sql
CopyEdit
Car(brand=’Toyota’, model=’Corolla’, year=2022)
This makes it easy to inspect objects without manually printing each attribute.
When Should You Use repr()?
You should use repr() when:
- You need an unambiguous and detailed representation of an object.
- You are debugging and need to see how an object is structured.
- You are working with logs where clarity is important.
- You want to customize how your class objects are represented.
If your goal is to provide readable output for users, str() is usually the better choice. But for debugging and object inspection, repr() is more useful.
Common Mistakes When Using repr()
Some common mistakes when using repr() include:
- Confusing repr() with str() – Remember, repr() is for debugging, while str() is for user-friendly output.
- Not defining __repr__ in custom classes – If you don’t define it, Python uses a default representation that may not be useful.
- Using repr() unnecessarily – Sometimes, str() is sufficient, and repr() might add unnecessary complexity.
To avoid these mistakes, always use repr() when debugging and str() when displaying information to users.
What Is repr in Python?
The repr() function is a built-in Python function that returns a string representation of an object. This representation is designed to be unambiguous and useful for debugging.

Why Do We Need repr() in Python?
We need repr() to:
- Get a precise and structured representation of an object.
- Debug and inspect objects effectively.
- Define how objects should be represented in our programs.
repr() vs str() – What’s the Difference?
Featurerepr()str()
Purpose Debugging and development User-friendly output
Output Unambiguous and detailed Readable and simple
Usage repr(object) str(object)
How to Use repr() in Python (With Examples)
Example with a list:
python
CopyEdit
my_list = [1, 2, 3]
print(repr(my_list))
Output:
csharp
CopyEdit
[1, 2, 3]
Example with a dictionary:
python
CopyEdit
my_dict = {“name”: “Alice”, “age”: 25}
print(repr(my_dict))
Output:
bash
CopyEdit
{‘name’: ‘Alice’, ‘age’: 25}
Using repr() in Classes (Magic Method __repr__)
The __repr__ method allows us to customize how objects of a class are represented. This is particularly useful when working with large projects where debugging is crucial.
By overriding __repr__, we ensure that our objects display meaningful information when printed.
The Bottom Line
The repr() function in Python is an essential tool for debugging and understanding objects. Unlike str(), which is meant for human-readable output, repr() provides an unambiguous and detailed representation of an object. Whether working with built-in data types or custom classes, using repr() correctly can improve the readability and maintainability of your code.