When looking at some python lines and functions, I discovered this strange python quirk:
s = "hello" print s[::-1]
which then prints: olleh
However, print s[len(s)-1:-1:-1] does not work. I understand that it must move from the last element s[len(s)-1] to the first element s[0] . However, it just prints an empty line '' , which, it seems to me, is due to the fact that in lines of some given length (say 5), s[4] == s[-1] . But I don't understand why python decides to use -1 instead of 4 , which is the actual len(s) .
In addition, s[len(s):0:-1] + s[0] works. Why is len(s) valid index? Does Python just convert len(s) to 0 arbitrarily?
PS This is in Python 2.7.8, I'm not sure if it also works in 3.xx
EDIT: Confirmed that Python 3 will be the same
source share