By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

Give List to Format String - How To

P: n/a
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

(I'm in Python 2.4 if that matters)

Thanks,

Greg

Sep 6 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Sep 5, 10:47 pm, "gregpin...@gmail.com" <gregpin...@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
Ah it just needs a tuple. I posted too soon I guess.
>>'%s aaa %s aa %s' % tuple(['test' for i in range(3)])
'test aaa test aa test'

-Greg
Sep 6 '07 #2

P: n/a
On Thu, 2007-09-06 at 02:47 +0000, gr********@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
Sep 6 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.