Here’s a sample of code:
s = girl
print len(s) # the length of s is 4, with valid indexes of 0 through 3
for i in range (0, len(s)):
The output is:
My question is: if the length of “girl” is 4, and I’m printing out the letters from 0 to 4, but the only valid characters of the string are indexed from 0 through 3, why isn’t an error thrown for a range violation? I’ve played with outputting in the range 0:len(s)-1 and get the letters “gir”, so it’s not that python is indexing a nul character at the end of the string in the code snippet above.
Why, when python strings are indexed from zero, is the character at s above the letter ‘l’? This can’t be true. What is true with regard to how this output is legal?