SabreWolfy SabreWolfy - 7 months ago 10
Python Question

Using Python String Formatting with Lists

I construct a string

s
in Python 2.6.5 which will have a varying number of
%s
tokens, which match the number of entries in list
x
. I need to write out a formatted string. The following doesn't work, but indicates what I'm trying to do. In this example, there are three
%s
tokens and the list has three entries.

s = '%s BLAH %s FOO %s BAR'
x = ['1', '2', '3']
print s % (x)


I'd like the output string to be:

1 BLAH 2 FOO 3 BAR

Answer
print s % tuple(x)

instead of

print s % (x)
Comments