Problem in Slicing

Why output is ’ ’ not ‘o’
in below image?

In Line 26…

First comes the starting index and than after colon comes the ending index.
For your example:
-1 index will refer to 4 index
-3 index will refer to 2 index.

So your code in line 26 is similar to writing a[4:2] and that’s the reason ‘’ comes as output.
If you want to print ‘o’ use a[-1:] which means print all characters after last character i.e. ‘o’

Hope this cleared your doubt. :blush:

So it should cause an error ?

No, no error will be caused but it will not give any output.

As 4 is starting index and it is included so it should atleast print ‘o’?

No because it has to start from index 4 and go uptill index 1(i.e. 2-1, or ending_index-1), but there is no pathway possible, and it does not print anything. This is not like something, that it should print atleast first character, its like print if complete path is possible else do not print anything.

thanks… doubt resolved.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.