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

eryk sun eryksun at gmail.com
Wed Apr 26 22:38:43 EDT 2017


On Thu, Apr 27, 2017 at 2:19 AM, Tim Peters <tim.peters at gmail.com> wrote:
> [boB Stepp <robertvstepp at gmail.com>]
>
>>  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'

In context this can be inferred from the docs, but I think f/F should
indicate the casing difference just like e/E, g/G, and x/X do.

https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting


More information about the Tutor mailing list