A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-repr-function/ below:

Python repr() Function - GeeksforGeeks

Python repr() Function

Last Updated : 26 Mar, 2025

The repr() function in Python is used to return a string representation of an object that can be used to recreate the object when passed to eval(). It is mainly used for debugging and logging because it provides an unambiguous representation of an object.

To understand more clearly, think of repr() as a blueprint for an object, while str() is a user-friendly description of it. Let's take an example:

Python
text = "Hello\nWorld"

print(str(text))

print(repr(text))

Output
Hello
World
'Hello\nWorld'

Explanation:

This shows that repr() is meant for debugging and object recreation, while str() is for user-friendly display.

Syntax

repr(object)

Parameters:

Return Type: repr() function returns a string (str) representation of the given object (<class 'str'>).

Examples of repr() method 1. Using repr() on Different Data-Types

Using repr() function on any data-type converts it to a string object.

Python
a = 42  # passing int
print(repr(a), type(repr(a)))

s = "Hello, Geeks!" # passing string
print(repr(s), type(repr(s)))

l = [1, 2, 3]   # passing list
print(repr(l), type(repr(l)))

st = {1, 2, 3}  # passing set
print(repr(st), type(repr(st)))

Output
42 <class 'str'>
'Hello, Geeks!' <class 'str'>
[1, 2, 3] <class 'str'>
{1, 2, 3} <class 'str'>

Explanation: repr(object) function returns a string representation of all the data-types, preserving their structure, and type(repr(l)) confirms that the output is of type str.

2. Using repr() with Custom Classes

__repr__ method in custom classes defines how an object is represented as a string. It helps in debugging by showing useful details about the object. Here's an example:

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

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

g = Person("Geek", 9)
print(repr(g)) 

Explanation:

3. Recreating Object Using eval()

In the previous example, we can recreate the Person object from the converted str object by repr() funtion using eval() function, here's how:

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

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

# Creating an object
p = Person("Alice", 25)

# Printing str and repr
print(str(p))
print(repr(p))

# Recreating the object using eval()
p_new = eval(repr(p))  
print(p_new)

print(p_new.name, p_new.age)

Output
Person('Alice', 25)
Person('Alice', 25)
Person('Alice', 25)
Alice 25

Explanation:

To know the difference between str and repr Click here.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4