-1
hello = "Hello there"
print(hello[10:0:-1])

This outputs:

ereht olle

And I can't figure out how or why.

print(hello[10:0:-2])

And this one outputs:

eetol

Could someone please help me to understand what the [10:0:-1] is actually doing?

Thanks!

zagd
  • 361
  • 1
  • 2
  • 9

1 Answers1

3

The notation is in order start, stop and step. So you start at index ten, go to index zero where the endpoint is exclusive with a step of negative two, meaning backwards two. Here is a diagram of indices:

# -------------------------------------------------------------------
# |  H  |  e  |  l  |  l  |  o  |     |  t  |  h  |  e  |  r  |  e  |
# -------------------------------------------------------------------
#    X           5           4           3           2           1
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70