## anonymous one year ago Regarding strings in Python, we know some_string[0] is the index of the first character of string some_string. Why is some_string[-1] the index of the last character instead of some_string[-0]? Would python evaluate -0 to 0 anyway?

• This Question is Open
1. Curry

Intuitively, 0 is the first element in the string. So -1 is the one before it, or in other words, the last element, cause it's wrapping. In other words, the negative sign doesn't signify subtraction. it just signifies the value of the number inside the brackets. so -0 = 0 and -1 = -1 and 1=1.

2. Curry

I think you got confused cause you thought the -0 minus one less than 0. but it doesn't, hope that helps.

3. Curry

to do that, i believe you'd use the actual subtract operator, like --. not just one -. Make sense?

4. anonymous

$$\color{blue}{\text{Originally Posted by}}$$ @ratinthecat Would python evaluate -0 to 0 anyway? $$\color{blue}{\text{End of Quote}}$$ This is one reason, but there is another reason though. Here is one way to think of python indexing for negative values: some_string[i] == some_string[len(some_string) + i] Since the len(...) - 1 gives the last index, the -1 gives the last element.

5. anonymous

These explanations really help. Thanks Wio and Curry!