[Tutor] To FORMAT or not to

Francois Dion francois.dion at gmail.com
Sun Jan 3 08:07:06 EST 2016


The answer is neither. The second shows the intent in part but doesn't
quite get it right.

The intent is to have a string template and insert values in that template:

print("You've visited {} & {}.".format(island, new)

This is totally clear what is going to happen. I'm not relying on the
behaviour of print() to format my string. Format does the formating, print
the printing :)

This separation of concern is a basic building block of good code and is
seen at various scale levels. At a higher level, it is seen in concepts
like MVC.

Another thing you do by having your string separated, is you could have
them defined elsewhere and have, say, a version in english and a version in
french.

I;m sure you can see the value.

Francois

On Sun, Jan 3, 2016 at 7:27 AM, yehudak . <katye2007 at gmail.com> wrote:

> Hi there,
> In a program I wrote the following line (Python 3.5):
>
> print("You've visited", island, '&', new + ".")
>
> A programmer told me that it's a bad habit, and I should have used instead:
>
> print("You've visited {0} {1} {2}{3}".format(island, "&", new, "."))
>
> May I understand why?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
raspberry-python.blogspot.com - www.pyptug.org - www.3DFutureTech.info -
@f_dion


More information about the Tutor mailing list