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

Alex Martelli aleaxit at
Fri Sep 10 08:27:44 CEST 2004

Pierre Fortin <pfortin at> wrote:
> I was hoping to use the likes of:  "%(key)*.*f" % map
> however, unlike with the non-(key) formats, there appears to be no way to
> specify a "*.*" size when a map is used...

It is not clear to me where you would expect to get the values for those
stars from -- 'map' being a dictionary it has no concept of "ordering",
so there is no concept of "next value" after map['key'].  If I had this
kind of problem I would doubtlessly tack it with _two_ formattings,
first one to get the values for the stars (with explicit names), giving
a clean format string, then another to get the value for the data.  Say:

'%%(key)%(width)d.%(prec)df' % map % map

Not all that readable, alas.  Alternatively, you could use a non-keyed
format string and extract things from the map on the RHS...:

'%*.*f' % [map[x] for x in ('key', 'width', 'prec')]

I personally consider this a bit more readable, but that's quite
debatable, of course...


More information about the Python-list mailing list