slice with negative stride

ajcppmod at gmail.com ajcppmod at gmail.com
Sat Oct 6 14:05:36 CEST 2007


I'm really confused about results of slices with negative strides. For
example

>>mystr = 'my string'

I would have then thought of the contents of mystr as:

indices    0 1 2 3 4 5 6 7 8
content    m y   s t r i n g

with mystr[:3] = 'my '

Can someone explain to me how mystr[:3:-1] = 'gnirt'?

I was expecting the result to be mystr[:3] reversed (' ym') i.e slice
then reverse or even the first 3 elements of the string after being
reversed ('gni') i.e. reverse then slice.

Thanks

Andy




More information about the Python-list mailing list