Page 1 of 1

Nested string substitution bug?

PostPosted: Thu Jan 01, 2009 6:59 am
by themaniac
Code: Select all
dict={'a1':'b'}
list=['a']
print   dict['[list[0]]1'] # prints 'b'
print '[dict['[list[0]]1']]' # gives compiler error: Expecting more string contents or the end of string after the bracketed expression.


Now, I know what I want the second print statement to do. Is the fact that it doesn't do what I want a bug or a feature?

Re: Nested string substitution bug?

PostPosted: Thu Jan 01, 2009 1:46 pm
by Charles
I don't consider it a bug. Switch your nested quotes from ' to "

And if interpolated expressions get too complicated, consider breaking them out as much for readability as anything else:
key = '[list[0]]1'
print '[dict[key]]'

Re: Nested string substitution bug?

PostPosted: Thu Jan 01, 2009 4:07 pm
by themaniac
I should have said that I did try both combinations of ' and " before posting with similar results. And the substitution is part of a list comprehension, so intermediate variables aren't possible (though I can obviously work around).