Slicing a String gives no output in python

Advertisements

When i run this code it returns nothing

name = "Azura"
print(name[-1:-6])

Why does this happen ?

>Solution :

Because you’re slicing everything in the string from index -1 to index -6. In the case of the string name, after converting negative index to positive, that is index 5 to index 0. In otherwords, when you do print(name[-1:-6]), you’re doing print(name[5:0]), which obviously doesn’t work, because python slices left to right. Therefore, if you want print(name[0:5]), just do print(name[-6:-1]).

TLDR: BAD print(name[-1:-6]) GOOD print(name[-6:-1])

Leave a ReplyCancel reply