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?
In python s[:1] first one is ignored but s[:3] in this the third alphabet is included? s is a string
Asked
Active
Viewed 151 times
-8

Willem Van Onsem
- 443,496
- 30
- 428
- 555

shiva
- 11
-
9Possible duplicate of [Explain Python's slice notation](http://stackoverflow.com/questions/509211/explain-pythons-slice-notation) – turbulencetoo Feb 21 '17 at 17:16
-
3When you specify `a[i:j]` it is always **`i` included**, and **`j` excluded**. – Willem Van Onsem Feb 21 '17 at 17:17
1 Answers
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