Reversing string of length 1 has some weirdness

Viewed 116

I am reversing a string in python though I don't want to reverse the complete string. I am using str[::-1] to reverse the string but excluding the last character.

For example: string = '0123456789' to be reversed to string = '987654321'.

However when the string length is 1 then problem arises with the reversing.

I simply used string=string[len(string)-2::-1]. It works fine for string having length greater than 1. But when string length is 1, python shows some weird results.

    w='0123456789'
    >>> w[len(w)-2::-1]
    =>'876543210'           #PERFECT
    >>> w[len(w)-2:0:-1]
    =>'87654321'            #PERFECT
    >>> w[len(w)-2:-1:-1]
    =>''                    #WHAT? - (1)
    >>> w[len(w)-2:-2:-1]
    =>''                    #WHAT? - (2)
    >>> w[len(w)-2:-3:-1] 
    =>'8'                   #WHAT? - (3)
    >>> w[len(w)-2:-4:-1]
    =>'87'                  #WHAT? - (4)
    #now see what happens when length of w is 1
    >>> w='a'
    >>> w[len(w)-2::-1]
    =>'a'                    #WHAT - (5) as len(w)-2 is -1 
    >>> w[len(w)-2:0:-1]
    =>''                     #PERFECT
    >>> w[len(w)-2:-1:-1]
    =>''                     #PERFECT as it's [-1:-1:-1]
    >>> w[len(w)-2:-2:-1]
    =>'a'                    #WHAT - (6)
    >>> w[len(w)-2:-3:-1]
    =>'a'                    #WHAT - (7)
    >>> w[len(w)-2:-4:-1]
    =>'a'                    #WHAT - (8)

when w='0123456789' why are the lines marked WHAT - (1) and WHAT - (2) not showing anything while the lines marked WHAT- (3) and WHAT - (4) start from 8 in reverse direction? When w='a' why are the lines marked WHAT - (5) shows 'a' when I am starting from -1 index and what is happening with the lines marked WHAT - (6), WHAT - (7) and WHAT - (8)?

1 Answers

w = '0123456789'

The index can be read as "from where (inclusive)":"to where (exclusive)":"step size". Negative elements in the first two sections imply a + len(...), so something like w[len(w)-2:-1:-1] is equivalent to w[-2:-1:-1] or w[-2:len(w)-1:-1].

Given len(w) == 10, w[len(w)-2:-1:-1] means w[8:9:-1]. This is obviously empty, since you can't get from 8 to 9 going backwards.

w[-2:-2:-1] is empty for the same reason (just as w[2:2] would be, for example).

w[len(w)-2:-3:-1] is w[8:7:-1], which is just a single character, '8', as expected. Similar reasoning for w[len(w)-2:-4:-1] mapping to w[8:6:-1].

w = 'a'

To explain w[len(w)-2::-1], you need one more piece of information: an empty start or stop index means "the limit in the appropriate direction, including the sign of the step size". So a missing stop index with a step of -1 means "past the beginning of the sequence". Also, keep in mind that len(w)-2 is -1 in this case. So you have w[-1::-1], which means go from the last element to the beginning backwards, i.e., 'a'.

w[len(w)-2:-2:-1] is simple if you keep track of indices as well: w[-1:-2:-1] is pretty much exactly equivalent to 1[-1::-1] when the length is 1.

The larger negative stop indices should be straightforward. There are no elements past the beginning of the string, so the result is always just 'a'.

You'd see more expected results if your start index was len(w)-3 or so, since in this case the len(w) just confuses the issue.

Related