Cheat sheet for the new string formatting?

Skip Montanaro skip.montanaro at gmail.com
Tue Jun 9 08:15:48 EDT 2015


Skip> Why don't floats support "{:.Ns}"? (I know I can use "{!s}".)

random832> Why would they? The old style didn't support %.Ns either.

Well, the old style does, though it appears the N is ignored:

>>> "%5s" % -0.00666762259822
'-0.00666762259822'

It doesn't raise an exception though.

(This is with Python 2.7.2. Haven't tried to see if that behavior
changed with 3.x.)

One thing which seems obvious now is that since format() delegates to
the individual types for formatting, much of the documentation of this
stuff must now be delegated to the individual types. However, I can't
find anything about the formatting syntax supported by
float.__format__.  Where might I find it documented that "{:.3}" is
synonymous with "%.3g"?

Skip



More information about the Python-list mailing list