[Tutor] Difference between %f and %F string formatting?

Tim Peters tim.peters at gmail.com
Wed Apr 26 22:19:48 EDT 2017


[boB Stepp <robertvstepp at gmail.com>]
> My Google-fu must be weak tonight.

Look here:

    https://en.wikipedia.org/wiki/Printf_format_string

>  I cannot find any discernible
> difference between '%f' % <some floating point number> and '%F' %
> <some floating point number>.  Is there any or do they duplicate
> functionality?  If the latter, why are there two ways of doing the
> same thing?

They differ only in the capitalization of the strings produced for
NaNs and infinities (math.nan and math.inf in Python 3).

>>> "%f" % math.nan
nan'
>>> "%F" % math.nan
NAN'
>>> "%f" % math.inf
inf'
>>> "%F" % math.inf
INF'


> I had a similar question for %d and %i, but googling suggests these
> are inherited from differences in handling input in the C language,
> though I could not locate a Python example where there is a need for
> one or the other.  Are there any relevant Python examples?

No difference for output in Python or in C.  Python inherited its
format codes from C, and so that's why Python allows both:  just
because C does.


More information about the Tutor mailing list