[Tutor] Beginners question

Cameron Simpson cs at cskk.id.au
Sat Apr 4 04:27:55 EDT 2020


On 03Apr2020 18:19, boB Stepp <robertvstepp at gmail.com> wrote:
>On Fri, Apr 3, 2020 at 6:35 AM Alan Gauld via Tutor <tutor at python.org> wrote:
>> Perhaps the worst example in Python is string formatting.
>> We now have at least 4 different ways of formatting strings.
>> That's at least 2 too many...
>
>Out of curiosity, which one(s) do you prefer to use and what shapes
>your preference(s)?

I tend to use % formatting with my debugging statements and log messages 
(which use %-formatting out of the box anyway). Eg:

    warning("badness! foo=%r", foo)

which will render "badness! foo=%r"%foo if the emit method fires. And in 
the same pattern my Pfx() message prefixer uses implicit percent 
formatting like the logging calls do:

    with Pfx("mkdir(%r)", dirpath):
      os.mkdir(dirpath)

which will render "mkdir(%r)"%dirpath is the prefix is used.

I do use modern f"{foo} blah" format strings something. They're 
particularly handy for template style strings because it is so easy to 
directly control where values land in the output. For example, I'm 
working on something at present which accepts a "-o format_string" 
command line option which lets the user specify the output message 
filled in from stuff from the programme.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list