Give List to Format String - How To

Carsten Haese carsten at uniqsys.com
Wed Sep 5 22:56:23 EDT 2007


On Thu, 2007-09-06 at 02:47 +0000, gregpinero at gmail.com wrote:
> I might just be being dumb tonight, but why doesn't this work:
> 
> >>> '%s aaa %s aa %s' % ['test' for i in range(3)]
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: not enough arguments for format string

To format multiple objects, the right operand must be a tuple. A list is
not a tuple, so '%' only sees one argument. You want something like
this:

>>> '%s aaa %s aa %s' % tuple('test' for i in range(3))
'test aaa test aa test'

HTH,

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list