I got this:
#slicing: [start:end:step]
s = 'I am not the Messiah'
#s[0::-1] = 'I'
So in this case
start=0, end=0, step=-1
Why is
s[0::-1] == 'I'
>>>> True
Because, -1
is a reversed stepping in this case.
Therefore when you say
s[0::-1]
You're actually going backward from position 0
to -1
where 0
is included
Therefore, returning I
in your case.
Note that when I say position 0
to -1
I mean that it will include position 0 and stop slicing after since a -1
index is not valid (which is different from reversed indexing like my_list[-1]
)
Because your slice starts with index 0 and steps -1 at a time, which means it hits the boundary immediately, leaving just the first item in the sequence, i.e. 'I'
, in the slice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With