In general, you want to use the second form ( .format()
), it is new and the other will eventually leave (at least that was the intention at some point - see Notes below).
To quote Python What's new in Python 3.0 docs:
The new system for embedded 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 a later time.) Read PEP 3101 for a complete scoop.
.format()
is available since at least Python 2.6
Additional Information about Advanced String Formatting (PEP 3101)
Notes:
@Duncan also mentions a stream link that discusses whether / when %
-based formatting will disappear in the comments below. And @NedBatchelder has this specific quote from Python 3.2 docs : " ... there are no current plans to deprecate printf-style formatting.
"
Levon source share