4

Possible Duplicate:
String formatting options: pros and cons

What's the difference between

"%.2f" % x

and

"{:.2f}".format(x)

I am a bit confused about which method should I use and for which version of Python.

Community
  • 1
  • 1
Vassilis
  • 2,801
  • 1
  • 20
  • 16

2 Answers2

7

In general you want to use the 2nd form (.format()) it's newer and the other one will eventually go away (at least that was the intention at some point - see Notes below).

To quote the Python What’s New In Python 3.0 docs:

A new system for built-in string formatting operations replaces the % string formatting operator. (However, the % operator is still supported; it will be deprecated in Python 3.1 and removed from the language at some later time.) Read PEP 3101 for the full scoop.

.format() has been available since at least Python 2.6

More information about Advanced String Formatting (PEP 3101)

Notes:

@Duncan also mentions a reference to a thread that discusses whether/when the % based formatting will go away in the comments below. And @NedBatchelder has this definite quote from the Python 3.2 docs: "... there are no current plans to deprecate printf-style formatting."

Levon
  • 138,105
  • 33
  • 200
  • 191
  • 1
    That quote is from the release notes of Python 3.0 so quite old. Note that Python 3.1 did not deprecate %-formatting and I think there was a decision taken that the older formatting will continue for the foreseeable future (but I can't immediately find the relevant reference). – Duncan Jun 19 '12 at 11:29
  • 1
    Ah, here's what I remembered: http://python.6.n6.nabble.com/formatting-depracation-td1861167.html Nick Coghlan wrote: "Without a systematic way to detect and convert %-style to {}-style formatting, enforcing the switch in the 3.x series just isn't practical. Heck, we still haven't figured out how to convert a lot of higher level APIs to the new scheme in a backwards compatible way (that's why modules like logging still default to %-style, with {}-style only available via options and wrapper objects)." – Duncan Jun 19 '12 at 11:32
  • @Duncan I recently went through those documents as I started to use v3.x myself. I only recall the quote that I mentioned, if you can find the other reference let me know and I'll update my answer with it. Thanks. – Levon Jun 19 '12 at 11:32
  • Thank you both for the answer and the comments! – Vassilis Jun 19 '12 at 11:35
  • @Duncan Thanks for the link, I added a note about it to my answer. – Levon Jun 19 '12 at 11:35
3

%-style tends to be briefer, but also more limited. .format() has some advantages:

  1. allows user-defined classes to provide their own formatting flags,
  2. can access attributes of objects

though in your example with floats, neither of those is an advantage.

Both of these techniques will continue to work, so which you use is up to you. There had been an idea that %-formatting would be removed from Python 3, but that is no longer true. See the 3.2 docs:

As the new String Formatting syntax is more flexible and handles tuples and dictionaries naturally, it is recommended for new code. However, there are no current plans to deprecate printf-style formatting.

Levon
  • 138,105
  • 33
  • 200
  • 191
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • Do you have a different/more definite link about %-formatting sticking around? If so I'd like to be able to add this to any future answers. I just recently started to dig into v3 and the .format() function specifically because I thought %-formatting (which I know from my C programming days) would be going away. Thanks. – Levon Jun 19 '12 at 12:59
  • @Levon: I added a link to the answer. – Ned Batchelder Jun 19 '12 at 13:10
  • Thanks .. that's definitely pretty definite :-) – Levon Jun 19 '12 at 13:12
  • I'll add a note about this to my current answer, I hope that's ok with you. – Levon Jun 19 '12 at 13:13