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

Diez B. Roggisch deetsNOSPAM at web.de
Thu Sep 9 23:53:13 CEST 2004


> Darn...  I was hoping that Python would let me format strings with
> mappings in a more readable/maintainable way than thusly:

Maybe you can simply combine the sequence and the map - if you don't have
numeric keys, that might work. Like this:

vals = ['first' , 'second']
map = {'foo' : 'bar'}

map2 = dict(map.items() + [(str(i), v) for i, v in enumerate(vals)])

print "%(0)s %(foo)s %(1)s" % map2

Not perfect, but IMHO better than your eval-based solution.

-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list