-1

Suppose s="mrunal"

I want to print run

for that I am doing print(s[1:4])

But in python why print(s[1:3]) is not available.

Simply I want to tell that why end in slicing is not that index which we want as last character?

s="mrunal"

index 0 = "m"

index 1 = "r"

index 2 = "u"

index 3 = "n"

index 4 = "a"

index 5 = "l"

here index for "n" is 3. Why end index is not same as s[1:3]? Why it is not inclusive? why it uses exclusive?

MRUNAL MUNOT
  • 395
  • 1
  • 5
  • 18
  • 1
    Read [this](https://stackoverflow.com/a/509295/4152153) answer, it may clear your doubts about slicing. – dcg Dec 24 '20 at 16:24
  • 1
    Does this answer your question? [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – Tom Myddeltyn Dec 24 '20 at 16:25
  • Half open intervals are easier to reason about. For example, `s[1:4] + s[4:] == s`. – bereal Dec 24 '20 at 16:25
  • `foo[0:5]` gives you up to 5 characters. Slices are often calculated values. `foo[n:n+2]` gives you up to 2. `foo[foo:foo+bar]` would give you 0 characters if `bar` is zero. Its usually the most natural way to do things. – tdelaney Dec 24 '20 at 17:29
  • @dcg @TomMyddeltyn It not providing my question's answer ```why it is?``` – MRUNAL MUNOT Dec 24 '20 at 17:29
  • @tdelaney but why exclusive? is any _specific reason_ for doing that exclusive? – MRUNAL MUNOT Dec 24 '20 at 17:31
  • I did. Its because adding X to the first index gives you X characters. – tdelaney Dec 24 '20 at 17:36
  • Consider other uses, `for index in range(5)` - it would be weird if this enumerated 6 things. `while index < len(sequence)` - note the _less than_ which keeps the index from overflowing the sequence. `mylist[len(mylist)]` is in an index error. A list's final element is `len - 1`. – tdelaney Dec 24 '20 at 17:41

2 Answers2

3

This isn't a great answer, but when dealing with indexing sequences, the upper bound is often exclusive, meaning it doesn't include the index itself.

Why? I've always assumed it was because the last index of a sequence is one less than the size, which let's you use the size as the upper bound without needing to offset it by 1:

s[1:] == s[1:len(s)]

Also note that the behavior is the same with range as well, where the upper bound of it is exclusive.

This also isn't specific to Python. Many languages use exclusive upper bounds; especially when dealing with indexing of sequences. It's a good thing to expect unless you read otherwise.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • 1
    This is I think the answer to the actual question: the OP is asking _why_ it works like this, not _how_ it works, so the duplicate isn't relevant. The answer is this - for consistency with `range()`, which in turn does it that way for consistency with older programming languages, for the reasons you laid out. – Green Cloak Guy Dec 24 '20 at 16:29
  • @Carcigenicate But why it does not take ```end``` as it is even if starts from zero. As I am shown example why end+1 not direct end? – MRUNAL MUNOT Dec 24 '20 at 17:12
  • @MRUNALMUNOT I tried to address that under "why". The last index is `size - 1`, so making it exclusive saves you from needing to subtract 1. It's the same with `for` loops in other languages using `<`. If you want to iterate an array by index in C/C++/Java, the for loop will look like `for (int i = 0; i < arr_len; i++)`. It uses `<` so that you don't need to `- 1`. It's the same idea. `<` is defining an exclusive upper bound. I'll incorporate that into my answer when I get a break. – Carcigenicate Dec 24 '20 at 17:25
-2

Edit:

Python, starts counting from 0. This means that if you need the first letter, you write 0. The second letter 1 and so on

s="mrunal"

index 0 = "m"

index 1 = "r"

index 2 = "u"

index 3 = "n"

index 4 = "a"

index 5 = "l"

This means you will need to take the second letter (index 1) to the fourth letter (index 4)