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

Cameron Simpson cs at zip.com.au
Thu Apr 27 03:23:19 EDT 2017


On 26Apr2017 22:51, Tim Peters <tim.peters at gmail.com> wrote:
>[boB Stepp <robertvstepp at gmail.com>, on %i/%d and %f/%F]
>> Hmm.  I'm surprised this slight distinction was worth keeping two
>> format codes that otherwise do the same thing.  Is there an actual
>> need for these due to Python being implemented behind the scenes in C?
>
>The implementation is irrelevant to this.  What is relevant:  Python
>took its format-string semantics from C.  Because of that alone, any
>deviation from what the C docs say has to have very strong
>justification.  "It's just like C" saves us from needing to write our
>own mountains of tutorials and reference material.   The C world -
>which has way more people and money supporting it - did that for us.

Another reason for this kind of thing is that if the printf codes are used 
faithfully WRT the C printf codes it makes reproducing specific outputs a bit 
easier.

It is a bit of a niche, but sometimes one wants to write something in Python 
whose output perfectly mimics that of another tool, usually because that output 
is in turn parsed by some utility one wants to use. If the target output was 
generated with printf, and if even better its source is available, one can just 
reuse the formats rather than having to laboriously reproduce special stuff.

Definitely a niche, and probably not the core motivation here. But handy.

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Tutor mailing list