formatted printing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ATHIRA P
    New Member
    • Mar 2012
    • 1

    formatted printing

    sir,
    Code:
    a=2.0/3
    print a
    print'a=%5.3f'%(a)
    in the above python code what is the use of 5 in 5.3.i think 3 is the number of decimal places.but i cant understand what is this 5 represnts.pleas e help me.
    Last edited by bvdet; Mar 23 '12, 11:41 AM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    The 5 in the 5.3 is the minimum field width. If a negative number, the padding occurs on the right.
    Code:
    >>> n = 20000000.123456789
    >>> print "n = %5.3f" % (n)
    n = 20000000.123
    >>> n = 0.123456789
    >>> print "n = %5.3f" % (n)
    n = 0.123
    >>> print "n = %50.3f" % (n)
    n =                                              0.123
    >>>

    Comment

    Working...