Addition to slice syntax (for strings)

Will McGugan news at NOwillmcguganSPAM.com
Mon Sep 13 13:18:41 EDT 2004


Hi,

I'd like to suggest an addition to the slice syntax. How about allowing 
strings as arguments to slices on strings, and interpret them as the 
index of the substring found within the string being sliced. I'm sure I 
didnt explain that too well, so here is an example..

 >>> a= "Find the string within the (parenthesis)."
#equivalent of a[ a.find('(') : a.find(')', a.find('(') + 1 ) + 1 ]
 >>> a['(':')']
'(parenthesis)'

#Strings with a negative step would search for the substring from the 
end of the string
 >>> a= 'some/folder/file.txt';
 >>> a['.',-1,-1]
'.txt'


I have actually implemented something similar in a C++ string class, so 
its not completely crazy - but I would be interested in what more 
experienced Python developers think of it.


Thanks,

Will McGugan





More information about the Python-list mailing list