{"@context":"https://schema.org","@type":"Article","mainEntityOfPage":{"@type":"WebPage","@id":"https://www.tutorialsteacher.com/python/repr-method"},"headline":"Python repr() Method","author":{"@type":"Organization","name":"TutorialsTeacher","url":"https://www.tutorialsteacher.com/aboutus"},"publisher":{"@type":"Organization","name":"TutorialsTeacher","logo":{"@type":"ImageObject","url":"https://www.tutorialsteacher.com/Content/images/logo.svg"},"sameAs":["https://www.facebook.com/tutorialsteacher","https://twitter.com/tutorialstchr","https://www.youtube.com/@tutorialsteacherOfficial"]},"dateModified":"2021-06-26T15:03:44.2239959Z","description":"The repr() method returns a string containing a printable representation of an object. The repr() function calls the underlying __repr__() function of the object."}repr() function calls the underlying __repr__() function of the object." />

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
Want to check how much you know Python?