Python repr() Method

The repr() method returns a string containing a printable representation of an object. The repr() function calls the underlying __repr__() function of the object.

Syntax:

repr(obj)

Parameters:

obj: Required. The object whose printable representation is to be returned.

Return Value:

Returns a string.

The following example demonstrates the repr() method.

Example: repr()
print(repr(10))
print(repr(10.5))
print(repr(True))
print(repr(4+2))

Output
'10'
'10.5'
'True'
'6'

The repr() function returns the string representation of the value passed to eval function by default. For the custom class object, it returns a string enclosed in angle brackets that contains the name and address of the object by default.

Example: repr()
class student:
	name=''

std = student()
repr(std)
Output
'<main.student object at 0x0000000003B1FF98>'

Override the __repr__() method to change this default behaviour, as shown below.

Example: repr()
class student:
	name=''
    def __repr__(self):
        return 'student class'
        
        
std = student()
repr(std)
Output
student class