Old-style %
formatting uses %
codes for formatting:
# A single value can be written as is:
'It will cost $%d dollars.' % 95
# Multiple values must be provided as a tuple:
"'%s' is longer than '%s'" % (name1, name2)
New-style {}
formatting uses {}
codes and the .format
method. Make sure not to mix and match – if the “template” string contains {}
placeholders, then call .format
, don’t use %
.
# The values to format are now arguments for a method call,
# so the syntax is the same either way:
'It will cost ${0} dollars.'.format(95)
"'{0}' is longer than '{1}'".format(name1, name2)