Vinay
Vinay

Reputation: 33

How to slice a string in Python in one step?

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.

Upvotes: 0

Views: 99

Answers (1)

Vovin
Vovin

Reputation: 760

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

Upvotes: 1

Related Questions