A good reference.<p>My take home is that every single version that is possible to do in old style formatting is longer in new style formatting. Even more so if the old-style formatting were written without the extraneous 1-ples.<p><pre><code> '%4d' % 42
</code></pre>
vs<p><pre><code> '{:4d}'.format(42)
</code></pre>
It isn't surprising that so much code is still being written in old-style. The advantages of the new style, like padding with arbitrary characters; value reuse (particularly for I18n, though that tends to use a different mechanism for production systems that need it); and nested display; are really quite minor.<p>This feeds into the other python issue on HN today - why does py3 have so little uptake? Because it is a solution to a problem few people have.
They missed my favorite trick!
locals() will give you a dict of, well, local variables, which generally coincides with what you want, so:<p><pre><code> a = 4
'A is: {a}'.format(**locals())
</code></pre>
works as expected.
My last remaining complaint with new formatting is with Pylint and logging messages. The old-style way is to write:<p><pre><code> logging.debug('some value: %d', 123)
</code></pre>
which avoids the cost of string interpolation if that log message wasn't going to be emitted, say because the log level is higher than DEBUG. If you instead write:<p><pre><code> logging.debug('some value: {}'.format(123))
</code></pre>
then Pylint will complain that I "Use % formatting in logging functions but pass the % parameters as arguments (logging-format-interpolation)".<p>Yes, I can disable that particular warning, either by maintain a pylintrc file or adding annotations to the source code. Yes, this is a dumb complaint. But yes, it still bugs me.
I really don't mind these apparent (small) improvements to Python.<p>However, the fact that Python 3 just went off and did it's own things rather than the usual cycle of:<p><pre><code> 1) improve a language feature
2) deprecate the old way of doing it
3) give people time to update code (usually a couple of point releases)
4) remove features that have been replaced
</code></pre>
Python 3 should not have dropped lots of little changes with no backwards compatibility. They should still make 2.8 and 2.9 that are releases that remove features and add new ones until most python code works in Python 3.
Python 2.7 also added a non-locale aware thousands separator that be be combined with other options:<p><pre><code> >>> "{:>40,.2f}".format(sys.maxint)
' 9,223,372,036,854,775,808.00'</code></pre>
<p><pre><code> Basic formatting
Old: '%d %d' % (1, 2)
New: '{} {}'.format(1, 2)
</code></pre>
It should rather be `{:d} {:d}'.format(1, 2)` but even that isn't strictly equivalent (try both styles with a float or a Decimal).
I'm a c dinosaur, so I've always used old-style formatting because I know it off by heart. Having said that, the alignment operator (<^>) reminds me of my joy when learning python - power and simplicity!
<p><pre><code> '%d %d' % (1, 2)
</code></pre>
I would have done this as<p><pre><code> '%s %s' % (1, 2)
</code></pre>
Because we're turning everything into a string at the end of the day anyway!<p>Yeah. Having finished the article, .format() just isn't really needed. If I'm at the point where I'm doing templating with key:values, I'll be using jinja.
I personally prefer the old style still, and I still don't see the value of deprecating it in favor of the new style. I started Python in 2008 (p3k was brand new), and I used new style formatting for about 2 years. After realizing that I was the only one using new style I switched and now I cannot imagines going back.
One caution that this document does not mention is that the new formatting is available only in recent versions of Python. One of my projects runs on a CentOS 5.x server, which has Python 2.4, so I had to convert all of the new style formatting to old in order to get it to run there.
I use the format:
foo = 'foo'
bar = 'bar'
print "%(foo)s%(bar)s" % locals()
which I guess is what this is similar too.<p>basically locals(), vars(), and globals() I find very useful for string formatting.
In Dash.app (on Mac), there's actually a nifty file that you can download that shows you how all the different formatting options work.<p>Its good, but I think this is even better.