Cheat sheet for the new string formatting?

Skip Montanaro skip.montanaro at gmail.com
Mon Jun 8 16:32:19 EDT 2015


This is counterintuitive:

>>> "{:.3}".format(-0.00666762259822)
'-0.00667'
>>> "{:.3f}".format(-0.00666762259822)
'-0.007'
>>> "%.3f" % -0.00666762259822
'-0.007'
>>> "{:.3s}".format(-0.00666762259822)
ValueError Unknown format code 's' for object of type 'float'

Why does the first form display five digits after the decimal point? Why
don't floats support "{:.Ns}"? (I know I can use "{!s}".)

This is using a freshly hg pulled and updated 2.7  branch.

Thx,

S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150608/d19df931/attachment.html>


More information about the Python-list mailing list