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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
.format()has been available since at least Python 2.6More 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.“