In Python, we use the repr()
function to return a string that contains a printable representation of an object.
repr(object)
As a parameter, this method takes the object
whose printable representation is required.
This method returns a string.
print(repr(5))print(type(repr(5)))print(repr(5.5))print(type(repr(5.5)))print(repr(False))print(type(repr(False)))print(repr(7+2))print(type(repr(7+2)))
When we pass the printable representation of an object to eval()
, we get the original object in return.
The code example given below explains this:
myString = "2 + 3"print(eval(repr(myString)))print(type(repr(myString)))
Free Resources