string formatting with mapping & '*'... is this a bug?

Pierre Fortin pfortin at pfortin.com
Thu Sep 9 20:06:49 CEST 2004


Hi!

"Python Essential Reference" - 2nd Ed, on P. 47 states that a string
format can include "*" for a field width (no restrictions noted); yet...

>>> "%*d" % (6,2)  # works as expected
'     2'

Now, with a mapping....

>>> mapping = {'one':1,'two':2}

These work as expected...

>>> "%(two)d" % mapping
'2'
>>> "%(two)6d" % mapping
'     2'

Attempting to specify a field width via '*' to a mapping fails....

>>> "%(two)*d" % (mapping,6)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: format requires a mapping
>>> "%(two)-*d" % (6,mapping)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: format requires a mapping

Am I expecting too much...?

Pierre




More information about the Python-list mailing list