% vs format
format
New in version 2.6.
See Format String Syntax for a description of the various formatting options that can be specified in format strings.
This method of string formatting is the new standard in Python 3, and should be preferred to the %
formatting described inString Formatting Operations in new code.
In [1]: '{}{}'.format(1, 2)
Out[1]: '12'
In [2]: '{0}{1}{0}'.format(1, 2)
Out[2]: '121'
In [3]: '{one}{two}'.format(one=1, two=2)
Out[3]: '12'
In [2]: '{one}{two}{one}'.format(one=1, two=2)
Out[2]: '121'
In [4]: '{{_}}{one}{two}'.format(one=1, two=2)
Out[4]: '{_}12'
In [5]: '{_}{one}{two}'.format(one=1, two=2)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-5-85a872d71e8b> in <module>()
----> 1 '{_}{one}{two}'.format(one=1, two=2)
KeyError: '_'
'{} {}'
vs.'{0} {1}'
- Str.format() for Python 2.6 gives error where 2.7 does not
Changed in version 2.7: The positional argument specifiers can be omitted, so '{} {}' is equivalent to '{0} {1}'.
- Str.format() for Python 2.6 gives error where 2.7 does not
%s
In [7]: '%s%s' % (1, 2)
Out[7]: '12'
UnicodeEncodeError
In [1]: '{}{}'.format('test', u'测试')
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-1-507ab7ed13b6> in <module>()
----> 1 '{}{}'.format('test', u'测试')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
In [2]: u'{}{}'.format('test', u'测试')
Out[2]: u'test\u6d4b\u8bd5'
In [3]: '%s%s' % ('test', u'测试')
Out[3]: u'test\u6d4b\u8bd5'
In [4]: from __future__ import unicode_literals
In [5]: '{}{}'.format('test', u'测试')
Out[5]: u'test\u6d4b\u8bd5'
References
[1] PyFormat@TT4IT, PyFormat —— Using % and .format() for great good!
[2] Docs@Python, 6.1. string — Common string operations —— 6.1.3. Format String Syntax