In Python 2.7.6, these both work:
>>> '{0} {1}'.format('hello', 'world')
'hello world'
>>> '{} {}'.format('hello', 'world')
'hello world'
but only one of these works:
>>> from string import Formatter
>>> Formatter().format('{0} {1}', 'hello', 'world')
'hello world'
>>> Formatter().format('{} {}', 'hello', 'world')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/string.py", line 545, in format
return self.vformat(format_string, args, kwargs)
File "/usr/lib/python2.7/string.py", line 549, in vformat
result = self._vformat(format_string, args, kwargs, used_args, 2)
File "/usr/lib/python2.7/string.py", line 571, in _vformat
obj, arg_used = self.get_field(field_name, args, kwargs)
File "/usr/lib/python2.7/string.py", line 632, in get_field
obj = self.get_value(first, args, kwargs)
File "/usr/lib/python2.7/string.py", line 591, in get_value
return kwargs[key]
KeyError: ''
Why are str.format and string.Formatter.format different in this way, though I haven't found anything in the documentation that implies this? Is this difference intended and/or documented anywhere?