Slice notation is written as follows:
list_name[start_index: end_index: step_value]
The list indexes in python are not like the numbers present on number line. List indexes does not go to -1st
after 0th
index when step_value
is -1
.
Hence below results are produced
>>>> print string[0:6:-1]
>>>>
And
>>>> print string[0::-1]
>>>> H
So when the start_index
is 0, it cant go in a cyclic order to traverse the indexes to -1, -2, -3, -4, -5, -6 for step_value
is -1
.
Similarly
>>>> print string[-1:-6:-1]
>>>> OLLEH
and
>>>> print string[-1::-1]
>>>> OLLEH
also
thus when the start_index
is -1 it goes in a cyclic order to traverse the indexes to -1, -2, -3, -4, -5, -6 to give output OLLEH
.
Its pretty straight forward to understand when start_index
is 6 and step_value
is -1
>>>> print string[6::-1]
>>>> OLLEH