3

I'm trying to understand regular expression operations, string slicing, and strings in Python.

String slicing using .start() and .end() results in the expected substring, and if I go for a single character from a string using .start() the resulting character is as expected, but if I go for a single character from a string using the .end() index, it doesn't result in the expected character.

I understand that lists (including strings) begin with element zero, but why are exceptions to this rule the stop index of a string slice and the re Match .end() index?

>>> import re
>>> m = re.search("bake","123bake456")
>>> m
<re.Match object; span=(3, 7), match='bake'>
>>> m.span()
(3, 7)
>>> m.start()
3
>>> m.end()
7
>>> "123bake456"[m.start():m.end()]
'bake'
>>> "123bake456"[m.start()]
'b'
>>> "123bake456"[m.end()]
'4'
  • 3
    Does this answer your question? [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – Woodford Apr 20 '21 at 17:18
  • In the answer they state "a[start:stop] # items start through stop-1". Thinking of "stop-1" is difficult. I prefer to think in absolutes when it comes to programming. I believe chemicalwill answered it with "goes up to" but doesn't include the end index* which is definitely in terms of absolutes. Thank you for answering nonetheless! – user3371644 Apr 20 '21 at 17:26
  • Woodford! The 4th answer down which starts "The answers above don't discuss slice assignment" explains what I needed! Slice assignment vs index assignment! – user3371644 Apr 20 '21 at 17:35

1 Answers1

4

The slice goes up to the ending index but does not include it. i.e. a span of (3, 7) includes index 6, but not 7. Similar to how range(1,100) would go 1-99 but not include 100.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
will-hedges
  • 1,254
  • 1
  • 9
  • 18