Explain the role of repr function.

  • Python can convert any value to a string by making use of two functions repr() or str(). 
  • The str() function returns representations of values which are human-readable
  • while repr() generates representations which can be read by the interpreter.
  • repr() returns a machine-readable representation of values, suitable for an exec command. 
  • Following code sniipets shows working of repr() & str() :
def fun():
   y=2333.3
   x=str(y)
   z=repr(y)
   print " y :",y
   print "str(y) :",x
   print "repr(y):",z

>>fun()
-------------
output
y : 2333.3
str(y) : 2333.3
repr(y) : 2333.3000000000002

No comments:

Post a Comment