Skip to content
Related Articles
Open in App
Not now

Related Articles

Python repr() Function

Improve Article
Save Article
  • Last Updated : 05 Aug, 2022
Improve Article
Save Article

Python repr() Function returns a printable representation of an object in Python.

Python repr() Function Syntax:

Syntax: repr(object)

  • object: The object whose printable representation is to be returned.

Return: str, printable representation of the object.

Python repr() Function Example

Python3




print(repr(['a', 'b', 'c']))


Output:

['a', 'b', 'c']

Example 1:  Passing string object to repr() method

Python3




strObj = 'geeksforgeeks'
 
print(repr(strObj))


Output:

'geeksforgeeks'

Example 2: Passing set object to repr() method

Python3




num = {1, 2, 3, 4, 5}
 
# printable representation of the set
printable_num = repr(num)
print(printable_num)


Output:

{1, 2, 3, 4, 5}

Example 3: Defining __repr__() method in class

Inside a class, we can define __repr__() method to override repr() function call behavior on the object of the class.

Python3




class Person:
    def __init__(self, name):
        self.name = name
     
    def __repr__(self):
        return f'Person("{self.name}")'
 
p = Person("Kiran")
# returns a string which can be used to recontruct Person object
print(repr(p))


Output:

Person("Kiran")

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!