0

I'm confused about the index in python. This is my problem:

number = "1234567"
print(number[-3:1:-3])

Output: 5

Why did the output is only 5 and not 52? When the starting position is in the number 5 and the end position is in the number 1(Am I right?). From the right(SINCE IT IS NEGATIVE SO REVERSE) i count from 5 then skipped 2 spaces then the number 2 should also be included.

deceze
  • 510,633
  • 85
  • 743
  • 889

1 Answers1

2

In the notation a:b:c, b is not included. So to achieve your desired output you should use:

print(number[-3:0:-3])
deceze
  • 510,633
  • 85
  • 743
  • 889
pygri
  • 647
  • 6
  • 17
  • Is it always b is not included during the a:b:c notation? – Francis Yvan Navarrosa Jul 09 '20 at 12:34
  • you can try it yourself and see how it works, remembering that -1 is the last element of the array :) what does `print(number[0:-1])` return? what about `print(number[0:-2])`? See how the last one is not included? – pygri Jul 09 '20 at 12:39