I am reading the wrox Beginning Python book. In chapter 2, the example states
>>> print "$.03f" & 30.00123
$30.001
However this does not explain the behavior of the command. Why use .03 when .3 and .003 etc have the same behavior in the shell? Furthermore, it does not explain what happends when I just use 3
>>> print "%.03f" % (2.34567891)
2.346
>>> print "%.003f" % (2.34567891)
2.346
>>> print "%.0003f" % (2.34567891)
2.346
>>> print "%.3f" % (2.34567891)
2.346
>>> print "%3f" % (2.34567891)
2.345679
I tried searching but couldnt get results as I dont know what the relevant keywords are.
The
0in'%.03f'does nothing. This is equivalent to'%.3f'.As for the second question, what does
'%3f'do, it simply specifies the length for the whole number, exactly as it would in'%3s'. It becomes clear if you make it longer: