Yet another Python textbook

Ian Kelly ian.g.kelly at gmail.com
Wed Nov 21 19:19:18 EST 2012


On Wed, Nov 21, 2012 at 4:21 PM, Joshua Landau
<joshua.landau.ws at gmail.com> wrote:
> "{}".format() is a blessing an "" % () should go. "%" has no relevance to
> strings, is hard to "get" and has an appalling* syntax. Having two syntaxes
> just makes things less obvious, and the right choice rarer.
>
> str.format is also really easy. I don't understand what makes you disagree.

I think it mostly depends on where you come from as a programmer.  As
you say, having two syntaxes muddles things up.  If you come from C or
C++, then the %s syntax feels natural and intuitive, and trying to
learn the sort-of-similar-but-not-really {} syntax on top of it is
just confusing.  Conversely, if you come from Java or C#, then the {}
syntax comes naturally, and having to learn %s in addition will give
one a headache.  And then there are those who come from Lisp and want
to know why they can't just use the familiarly easy ~a syntax.

None of these are really any easier than the others.  But they are
sort of similar at a superficial level, which just makes it that much
more painful to learn one when you're already accustomed to another.

I think my favorite example from the str.format documentation is this
one.  Apart from demonstrating the flexibility of the format system,
it also manages to mix the two systems in a most unholy way:

>>> import datetime
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2010-07-04 12:15:58'



More information about the Python-list mailing list