difference: repr(a) and str(a)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jenya56
    New Member
    • Nov 2009
    • 14

    difference: repr(a) and str(a)

    In my program, I have value a=0.24
    when I say : str(a) it prints 0.24
    when I say: repr(a) ir print '0.24'
    Why does str(a) not work? Thanks
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Code:
    In [1]: a=0.24
    
    In [2]: str(a)
    Out[2]: '0.24'
    
    In [3]: repr(a)
    Out[3]: '0.23999999999999999'
    
    In [4]: print str(a)
    ------> print(str(a))
    0.24
    
    In [5]:
    So it probably is working, depending whether or not you used the print command (which drops the quotes from a string).

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      See the Python documentation for an explanation of str() and repr(). From the docs:
      "Many values, such as numbers or structures like lists and dictionaries, have the same representation using either function. Strings and floating point numbers, in particular, have two distinct representations ."

      Comment

      Working...