0

Suppose I was slicing a list and a string:

num_list = [1, 2, 3, 4]
print(num_list[2:]) # => [3, 4]

text_string = 'This is a test string'
print(text_string[5:] # => 'is a text string'

So, when I slice the list, the first slice index is inclusive, but when I slice the string, the slice index is noninclusive. What is it like this? Why does string slicing not follow the same ruleset that list slicing appears too?

My understanding of list slicing is that the left slice index is always inclusive, whilst the right slice index is only inclusive when it is left blank.

Isthisathing
  • 15
  • 1
  • 5
  • What do you mean by inclusive/noninclusive? – melpomene Jun 24 '16 at 17:41
  • `num_list[2] => 3` / `text_string[5] => 'i'`. No difference. – spectras Jun 24 '16 at 17:42
  • Indexing is the same for both string and list. Indexing in Python starts at zero. – Mark Mikofski Jun 24 '16 at 17:42
  • 1
    I don't understand what you're saying. 3 is index 2 of the list, and slicing starts from there; `i` is index 5 of the string, and slicing starts from there. Slices are always half-open; the lower index is inclusive, the upper index is exclusive. – Daniel Roseman Jun 24 '16 at 17:42
  • No, you haven't demonstrated that at all. Slices work how you expected, you've misinterpreted the results you're seeing. Indices are zero based, and slices include `start` but exclude `end`. – jonrsharpe Jun 24 '16 at 17:42
  • how are you counting?...you should count from 0 in both cases – danidee Jun 24 '16 at 17:43
  • @Isthisathing welcome to SO! Your question was probably downvoted because it appears you did not sufficiently search for an answer before posting since the answer is easily found by reading the [Python Tutorial](https://docs.python.org/2/tutorial/). You might be interested in reading [How do I ask a good question](http://stackoverflow.com/help/how-to-ask) where you'll see that questions demonstrating the OP has first searched for an answer are more highly regarded, and "do-it-4-me" are despised. SO _is_ a great reference, but it will become cruft if saturated with trivial queries, so be wise. – Mark Mikofski Jun 24 '16 at 18:03

2 Answers2

3

They both behave exactly the same.

For some reason you expect the list's indexes to start from 0 and
the string's indexes to start from 1.

The fact is that they both start at 0.

As @Mark suggested in the comments, strings and lists indexing from Python's documentation.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • 1
    IMO a visual will help. Also links to [indexing strings in the Python Standard Library Tutorial](https://docs.python.org/2/tutorial/introduction.html#strings) where it says, "Strings can be indexed (subscripted), with the first character having index 0." and there's a fancy visual of "Python" indexed in a table. [Indexing lists is also described in the same tutorial](https://docs.python.org/2/tutorial/introduction.html#lists). I think it's important to teach new poster how to find answers themselves. – Mark Mikofski Jun 24 '16 at 17:46
  • 1
    @MarkMikofski I agree, adding the link. – DeepSpace Jun 24 '16 at 17:47
0

But it is the same! Insert indices before each element: [0=1,1=2,2=3,3=4][2:] means: from index 2 until end [0=T,1=h,2=i,3=s,4= ,5=i,6=s,...][5:] means: from index 5( in this case from the letter 'i') until the end.

Gábor Fekete
  • 1,343
  • 8
  • 16