-2

When i run this code it returns nothing

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

Why does this happen ?

2 Answers2

0

If you want to use a minus index that's going from right to left you must always use a negative step

print(name[-1:-6:-1])

The direction of the slicing must be the same as the direction of the steps.

Also it's easier to use string[::-1] if you want to reverse a string.

0

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])

Yimin
  • 335
  • 1
  • 8