String interpolation question

Fernando Pérez fperez528 at yahoo.com
Mon Apr 15 16:19:44 EDT 2002


Hi group,

I have a string interpolation problem and can't seem to find a clean solution.

Suppose I have a list and an index:

y=[5,6]
i=1

I want to print y[i]. The following fails:

print '%(y[%(i)s])s' % locals()

as does the simpler

print '%(y[%(i)s])s' % locals()

Obviously this works:

print '%s' % y[i]

but I don't want that because in my real problem, the string is long and I 
don't want the dangers of positionally matched %s arguments.

By the way, Itpl interpolation here also crashes:

printpl('$y[$i]')

gives an exception.

I know that pep215 never gets anywhere everytime it gets pushed, but I 
_really_ feel that string interpolation is about the only place where python 
simply sucks (IMHO ;) I keep running into situations where the only solutions 
that work are either kludgy or require way too much work to be clean.

Thanks for any help,

f.



More information about the Python-list mailing list