0

Here is a sample piece of code I wrote using string indexing, but I'm having trouble with these two outcomes. I highlighted what I thought would be the answer to the first one but I don't understand at all the second output. I assumed the string indexing was similar to range() function, but it seems slightly different. Here is my sample code:

word = "Hello There"

print(word[5:3:-1])
# olle
print(word[::-1])
Cameron
  • 15
  • 4

1 Answers1

0
word = "Hello Ther"     #i remove 'e' at the end for beeing more clear

# any string character has its onw index 
    # Hello Ther
    # 0123456789    - indx

# as I can guess the only thing you don't understand is that when you 'go backward' with [::-1] charackter's indeces are not changed 
# so this [5:3:-1] mean 
    # you start on 5 index inclusevely == ' ' (whitespace)
    # you stop on 3 index exclusevely == 'l' (the second 'l')
    # and in the end you get ' o'   
                            # 54    - indx
Dmitriy Neledva
  • 867
  • 4
  • 10