This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eric.smith
Recipients LambertDW, eric.smith, ezio.melotti, gvanrossum, mark.dickinson, ncoghlan, orsenthil, pitrou, terry.reedy
Date 2009-03-14.01:35:37
SpamBayes Score 6.227463e-12
Marked as misclassified No
Message-id <>
I believe this patch is complete. I need to add tests and documentation,
but the code itself should be finished.

Here's the normal case:
>>> '{} {}'.format('test', 0)
'test 0'

It also handles error checking:
>>> '{1} {}'.format('test', 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot switch from manual field specification to automatic
field numbering
>>> '{} {1}'.format('test', 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot switch from automatic field numbering to manual field

You can use named fields along with auto-numbered fields:
>>> '{test} {}'.format(1, test=2)
'2 1'

You can nest either named or auto-numbered fields:
>>> '{:^{}}'.format('x', 10)
'    x     '
>>> 'pi={:{fmt}} {:{fmt}}'.format(3.1415, 2.71828, fmt='1.4f')
'pi=3.1415 2.7183'

Attribute access is supported:
>>> '{.__abs__}'.format(0)
"<method-wrapper '__abs__' of int object at 0x85db8f8>"

As is subscripting:
>>> '{[x]}'.format({'x':4})
>>> '{[1]}'.format([1, 2])

I'll work on the tests over the weekend, then commit to trunk and py3k.

We need to decide what to do about string.Formatter (which I just
realized I erroneously called string.Format in the previous message).
Date User Action Args
2009-03-14 01:35:44eric.smithsetrecipients: + eric.smith, gvanrossum, terry.reedy, mark.dickinson, ncoghlan, orsenthil, pitrou, LambertDW, ezio.melotti
2009-03-14 01:35:43eric.smithsetmessageid: <>
2009-03-14 01:35:42eric.smithlinkissue5237 messages
2009-03-14 01:35:41eric.smithcreate