-8

Why in python while slicing a string suppose s='hello' we run command s[1:] and the result is 'ello'.First alphabet is ignored.But s[:3] gives result 'hel'. 3rd alphabet should be ignored?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
shiva
  • 11

1 Answers1

1

This is slicing notation. The first index is included, and the last index is excluded.

When you do [1:], the first letter isn't ignored. The first letter has an index of 0, so you literally told the notation to include everything from the second letter to the end. When you do [:3], the indices 0, 1, and 2 (corresponding to h, e, l) are included. The fourth letter (index 3) is not included.

spicypumpkin
  • 1,209
  • 2
  • 10
  • 21