0

I want to print the part of the text in reverse order. Help, why is my code not working?

s = 'abch12345h'
print(s[3:9:-1])
Pinta450
  • 3
  • 3

2 Answers2

1

Since your stepping backwards your starting and ending index needs to switch place like this

print(s[9:3:-1])

>>> h54321

Here's a good related answer: Understanding slicing

Mandera
  • 2,647
  • 3
  • 21
  • 26
0

Done! If the stride is negative, then the start index must be more than the end index. Is will work:

print(s[9:3:-1])
Pinta450
  • 3
  • 3
  • 2
    There's no need to answer your own question when an equivalent acceptable answer is already given. Just click the check mark on the left to accept it. – bereal Sep 02 '22 at 14:19
  • I would agree that this is a redundant answer, but I'm glad you found what you needed :) – Shawn Hemelstrand Sep 08 '22 at 00:29
  • I agree. I didn't know about Mandera's answer when I was writing my answer. The different is 3 minutes. Thanks! – Pinta450 Sep 09 '22 at 06:18