Python repr()

In this post, we will see about python repr function.
repr method returns canonical string representation of the object.repr method is generally used for development and debugging purposes.
repr method takes a single parameter.


obj – object whose canonical representation has to be returned.


repr returns a string that would be of same value as passed to eval().eval() calculates the result of an expression. The result is always a value (such as a number, a string, or an object)
For many object types, including most builtins, eval(repr(obj)) == obj.
Goal of repr is to be unambigous.The official Python documentation says __repr__ is used to compute the “official” string representation of an object.


Let’s take an example:

st='Hello' repr(st)



If you notice, it returns “‘Hello'” in double quotes as it returns String ‘Hello’.
repr actually calls a method __repr__ of st, which returns the string containing the printable representation of the value ‘Hello’ assigned to st. So it returns ‘Hello’ inside the string “” resulting in “‘Hello'”. The idea of repr is to return a string which contains a series of symbols which we can write in the interpreter and get the same value which was sent actaully as an argument to repr.

That’s all about Python repr method.

Leave a Reply

Your email address will not be published. Required fields are marked *