Python 2.6 introduced the str.format() method with a slightly different syntax from the existing % operator. Which is better and for what situations?
The following uses each method and has the same outcome, so what is the difference?
#!/usr/bin/python sub1 = "python string!" sub2 = "an arg" a = "i am a %s" % sub1 b = "i am a {0}".format(sub1) c = "with %(kwarg)s!" % {'kwarg':sub2} d = "with {kwarg}!".format(kwarg=sub2) print a # "i am a python string!" print b # "i am a python string!" print c # "with an arg!" print d # "with an arg!"Furthermore when does string formatting occur in Python? For example, if my logging level is set to HIGH will I still take a hit for performing the following
%operation? And if so, is there a way to avoid this?log.debug("some debug info: %s" % some_info)
%style more often, because if you do not need the improved capabilities of theformat()style, the%style is often a lot more convenient. – Lutz Prechelt Apr 12 '16 at 9:24format()formatting style and the older%-based formatting style. – Lutz Prechelt Apr 12 '16 at 9:26