Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String formatting: better to use '%' or 'format'? [duplicate]

Tags:

python

string

I use python 3.4 and I can format strings in two ways:

print("%d %d" %(1, 2))

and

print("{:d} {:d}".format(1, 2))

In documentation they show examples only using 'format'. Do it mean that using '%' is not good, or it does not matter which version to use?

like image 209
user3654650 Avatar asked Dec 13 '25 00:12

user3654650


1 Answers

Quoting from the official documentation,

This method of string formatting is the new standard in Python 3, and should be preferred to the % formatting described in String Formatting Operations in new code.

So, format is the recommended way to do, going forward.

like image 155
thefourtheye Avatar answered Dec 15 '25 12:12

thefourtheye