tags:

views:

77

answers:

2

Is there a simple way of passing a list as the parameter to a string substitution in python ? Something like:

w = ['a', 'b', 'c']

s = '%s\t%s\t%s\n' % w

Something similar to the way dictionaries work in this case.

+3  A: 

Just convert the list to a tuple:

w = ['a', 'b', 'c']
s = '%s\t%s\t%s\n' % tuple(w)
Amnon
Great, didn't know about this one, thanks!
hyperboreean
And of course, as fabe said, you can just use a tuple directly.
Amnon
It depends from where that data is coming from :-), but yes, theoretically it's possible.
hyperboreean
A: 

use a tuple instead of a list

w = ('a', 'b', 'c')
s = '%s\t%s\t%s\n' % w

using a dict also works

w = { 'Akey' : 'a', 'Bkey' : 'b', 'Ckey' : 'c' }
s = '%(Akey)s\t%(Bkey)s\t%(Ckey)s\n' % w

http://docs.python.org/release/2.5.2/lib/typesseq-strings.html

fabe