Making string-formatting smarter by handling generators?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Wed Feb 27 15:11:32 EST 2008
En Wed, 27 Feb 2008 14:23:49 -0200, Tim Chase
<python.list at tim.thechases.com> escribi�:
> Is there an easy way to make string-formatting smart enough to
> gracefully handle iterators/generators? E.g.
>
> transform = lambda s: s.upper()
> pair = ('hello', 'world')
> print "%s, %s" % pair # works
> print "%s, %s" % map(transform, pair) # fails
>
> with a """
> TypeError: not enough arguments for format string
> """
Note that your problem has nothing to do with map itself. String
interpolation using % requires either many individual arguments, or a
single *tuple* argument. A list is printed as itself.
py> "%s, %s" % ['hello', 'world']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
py> "%s" % ['hello', 'world']
"['hello', 'world']"
So the answer is always use tuple(...) as others pointed.
--
Gabriel Genellina
More information about the Python-list
mailing list