Home > Net >  How to slice a string in Python in one step?
How to slice a string in Python in one step?

Time:04-06

Am new to Python. "Hello World"[0:5][::-1] prints "olleH" as expected. I was wondering why "Hello World"[0:5:-1] is not doing the same.

CodePudding user response:

Because you should start with a larger index and end with a smaller one in the case of a negative step. So the equivalent of your example is "Hello World"[4::-1].

  • Related