|
When selecting a sequence of chars in a string you use string[start:stop]. Why was <start> designed as inclusive and <stop> as exclusive? |
|
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). |
|
One way to think about this is to think about the indexes being between the letters. Like this:
is
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. |