When selecting a sequence of chars in a string you use string[start:stop].
Why was <start> designed as inclusive and <stop> as exclusive?
asked 16 Mar '12, 20:20
One reason has to do with lists and strings being indexed from 0 instead of 1 (and most languages out there do this--not just Python, blame C for this). Thus, if you did a substring of str[0:len(str)], you get the entire string.
Another reason is for processing strings. For example, suppose you want to take substrings at index A, B, C, then you would have str[0:A], str[A:B], str[B:C], str[C:]. I don't have to add plus 1 everywhere.
Finally, it also allows you to take substrings that are empty strings, e.g. str[A:A], although I can't think of a good reason off-hand.
Sometimes string functions prefer start index and length instead (I prefer this, but start and end + 1 seems more common).
answered 16 Mar '12, 21:37
Fundamentally, the answer is "because that's the choice that the language author made." I agree with you that it's an odd choice, but then I'm used to a language that does it the other way. (Panorama PanTalk. It also uses a semicolon instead of the colon.)
There's no hope of reversing the decision, but we are free to think in terms of a different name for the second parameter. You could call it "upto" or "limit" or "until", for instance.
EDIT: On later thought, "startscan" and "stopscan" seem like good mnemonic names for the two parameters. If one were really bugged by the built-in syntax, one could write a wrapper function that adds 1 to its second parameter and then calls the built-in version. A bit slower to execute, of course, but Python isn't the right language for high-performance computation.
One way to think about this is to think about the indexes being between the letters. Like this:
This gives a reasonable model for the examples above - things like
...and so on. If you want, you can see every index as a running count - How many characters has there been in this string before this slot? Also, it explains how you can start from 0 and still slice with the length of the string.