Give List to Format String - How To

G

gregpinero

C

Carsten Haese

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:
'test aaa test aa test'

HTH,
 
B

Ben Finney

I might just be being dumb tonight, but why doesn't this work:

Congratulations for finding the answer quickly, and thank you for
letting us know the answer.

In future, if you want to ask "why doesn't this work", please show us
all three of: the example code, the actual behaviour, and the expected
behaviour. "Doesn't work" is not a substitute for the last two.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,888
Messages
2,569,964
Members
46,294
Latest member
HollieYork

Latest Threads

Top