Addition to slice syntax (for strings)

Will McGugan news at
Mon Sep 13 19:18:41 CEST 2004


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['(':')']

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

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.


Will McGugan

More information about the Python-list mailing list