educative.io

Why does this return nothing in slicing

my_string = "This_is_MY_string!"
print(my_string[11:5:3])

Hello @Subashanan_Nair,
You are right, it is not returning anything.
According to your code, we want it to start from the 11th index and end at the 5th index, but the step range you gave is positive. It should be negative value as starting from the 11th index and ending at the 5th index.

 my_string = "This_is_MY_string!"

 print(my_string[11:5:-3])
 
# or

 print(my_string[11:6:-1])

It works in reverse as well, but for it to work, the step value should also be negative.

2 Likes

Thank you so much for your reply. I understand the reason why it’s not returning anything.