-3

course = 'Python for Beginners'

print(course[-16:-1])

What is the result?

Explain the result please.

I tried to figure out the result without success.

Barry
  • 1

1 Answers1

0

The slice notation [-16:-1] means go from -16 to -1 (not inclusive) in steps of +1, so it's accessing indices -16, -15, -14, -13, ... -2 in that order.

Negative indices means to look from the end of the string and look backwards, so -1 is the last character, -2 is the second-to-last character, and so on. If you count 16 characters from the end of "Python for Beginners", you get the "o" in "Python". After that, since we're counting by +1 at that point, we just grab the next letters until index -2 which is the "r" in "Beginners".

The final output is on for Beginner

Michael Cao
  • 2,278
  • 1
  • 1
  • 13