What's the difference between these 2 statements?

Peter Otten __peter__ at web.de
Wed Apr 20 15:53:49 EDT 2005


ahmedt at gmail.com wrote:

> so if I want to reverse it fully using s[len(s)-1:x:-1] what would x be
> or is it impossible to express it in this way ?

This does not work for integers, because the theoretically correct value 
x = -1 already has another interpretation as the gap between the last and
the last but one character. Here are two workarounds:

1. Set x to None

>>> s = "12345"
>>> s[len(s):None:-1]
'54321'

2. Separate slicing operation and reversal:

>>> s = "12345"
>>> s[0:len(s)][::-1]
'54321'

Peter




More information about the Python-list mailing list