> Which is the preferred way of string formatting? > > (1) "the %s is %s" % ('sky', 'blue') > > (2) "the {0} is {1}".format('sky', 'blue') > > (3) "the {} is {}".format('sky', 'blue') > > As I know (1) is old style. (2) and (3) are new but (3) is only > supported from Python 2.7+. > > Which one should be used?
Sometimes, I use both ;-) That can save you from ugly escapes such as %%s or {{0}}. Here's an example from the standard library: http://hg.python.org/cpython/file/7254c03b7180/Lib/collections.py#l235 Note the template has both {typename} formatting for the first pass and %r style formatting in the generated code. Raymond ------------ follow my tips and recipes on twitter: @raymondh -- http://mail.python.org/mailman/listinfo/python-list