What's the difference between these 2 statements?

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Wed Apr 20 15:33:08 EDT 2005


tiissa wrote:
> Reinhold Birkenfeld wrote:
>> ATSkyWalker wrote:
>> 
>>>What's the difference between these 2 statements?
>>>
>>>If you have a String s="12345"
>>>
>>>s[len(s)::-1] = "54321"
>>>
>>>But
>>>
>>>s[len(s):0:-1] = "5432"
>>>
>>>Why? What's the difference? What number then can I use as the end of
>>>the slice if I were to supply all 3 parameters?
>> 
>> 
>> -1.
> 
> -len(s) or less.
> -1 will return an empty string.
> 
> Actually you start from len(s)-1 (len(s) is not an index in s) and you 
> stop when you reach the index specified (or the end). Since -1 is the 
> same index as the starting one (-1~>len(s)-1, -2~>len(s)-2, 
> -len(s)+1~>0), you end up with an empty string.
> 
> Therefore you have to try to reach indices lower (due to the negative 
> step) than the minimum valid index of your list in order to reverse it 
> fully.

Right, sorry.

Well, I guess that's why one can leave out the index...

Reinhold



More information about the Python-list mailing list