[issue5237] Allow auto-numbered replacement fields in str.format() strings

Eric Smith report at bugs.python.org
Fri Feb 13 19:36:56 CET 2009


Eric Smith <eric at trueblade.com> added the comment:

Terry J. Reedy wrote:
> Terry J. Reedy <tjreedy at udel.edu> added the comment:
> 
> All I am requesting is that
> '{} {} {}'.format(3, 'pi', 3.14) work as
> 
>>>> '%s %s %s' % (3, 'pi', 3.14)
> '3 pi 3.14'
>>>> '{0} {1} {2}'.format(3, 'pi', 3.14)
> '3 pi 3.14'
> 
> do today (3.0).

My string.Formatter subclass (attached to this bug report) does do this:

$ ./python.exe 
Python 2.7a0 (trunk:69516, Feb 11 2009, 14:30:31) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from auto_number_formatter_1 import MyFormatter
>>> f = MyFormatter()
>>> f.format('{} {} {}', 3, 'pi', 3.14)
'3 pi 3.14'
>>> 

This is just for vetting the concept, if it's accepted I'll modify
''.format(). It's not a huge change. I just want to make sure that this
implements what people are expecting.

The talk about '{:d}' and the like is just to make sure all the cases
are addressed. I doubt it would often be used that way.

> I should note that the difference between typing {}, which is easy, and
> {1}, is more than just one keystroke because the latter requires
> unshift-1-shift

Agreed.

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5237>
_______________________________________


More information about the Python-bugs-list mailing list