4

I want to get the last character of the string. I can print it using len method to get it BUT I can't compare it to another character while getting it using the VERY SAME method. Now I'll just paste this WTF code here. Please tell me what's going on here.

fields = line.split(';')
last = fields[0]
capacity = fields[1]
region = fields[2]

print(capacity)
print('kek')
print(capacity[len(capacity)-1])
print('even more kek')
while capacity[len(capacity)-1] == '0':
    capacity = capacity[1:len(capacity)-2]
    last = last[1:len(last)-2]

Now, the output

100000
kek
0
even more kek
Traceback (most recent call last):
  File "/home/mekkanizer/Документы/proj/rossvyaz/making_insert_queries.py", line 52, in <module>
    while capacity[len(capacity)-1] == '0':
IndexError: string index out of range
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
mekkanizer
  • 722
  • 2
  • 9
  • 28
  • Why don't you just use `capacity[-1]`? Also, note what happens when `len(capacity) == 0`; `''[-1]` isn't going to work. – jonrsharpe Feb 07 '15 at 21:16

4 Answers4

6

You ended up with an empty string, so there is nothing to index anymore:

>>> ''[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

You do so by shortening capacity in the while loop:

capacity = capacity[1:len(capacity)-2]

You start with 100000, then make that '000', then '':

>>> capacity = '100000'
>>> capacity[1:len(capacity)-2]
'000'
>>> capacity[1:len(capacity)-2][1:len(capacity)-2]
''

You may have missed you are doing this as you are never printing the value of capacity in the loop.

You can always index from the end using negative indices:

>>> 'last character'[-1]
'r'

This works for slicing too:

>>> 'last character'[5:-4]
'chara'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks for pointing out the method with negative indices BUT please explain how did I end with an empty string if I actuallly CAN print it's last character one line of code before and I do NOT change the string anyhow. – mekkanizer Feb 07 '15 at 21:19
  • @mekkanizer: you do change the value of `capacity`. In the loop you assign it to `capacity[1:len(capacity)-2]`. – Martijn Pieters Feb 07 '15 at 21:20
1
Traceback (most recent call last):
  File "/home/mekkanizer/Документы/proj/rossvyaz/making_insert_queries.py", line 52, in <module>
    while capacity[len(capacity)-1] == '0':
IndexError: string index out of range

The only case in which this would happen is when capacity is an empty string, and len(capacity)-1 is therefore -1, which is not a valid index into an empty string.

This does not happen during the first iteration of the loop (you've checked for that), but evidently does happen during a subsequent iteration, after you've shortened capacity.

My advice would be to read up on Python's slicing notation, in particular using negative indices to refer to the end of the string. One good resource is Explain Python's slice notation

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Thanks for pointing out the method with negative indices BUT please explain how did I end with an empty string if I actuallly CAN print it's last character one line of code before and I do NOT change the string anyhow. – mekkanizer Feb 07 '15 at 21:20
  • 1
    @mekkanizer: You print it *before* the loop, but then change it *inside* the loop. – NPE Feb 07 '15 at 21:21
1

In Python, you can get items numbered from the end of a sequence by using a negative index. Correspondingly, the last item has the index -1. That is, your construct capacity[1:len(capacity)-2] is not needed at all. The others have pointed out what actually happened.

Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130
1

You can use slicing notation which will return "" for an empty string:

while capacity[len(capacity)-1:] == 0

Or use while capacity to catch empty strings:

while capacity and capacity[-1] == "0":

You keep shortening capacity when reassigning capacity in your while loop so eventually it will be an empty string:

 capacity = capacity[1:len(capacity)-2] 

foobar foobar <- start
oobar foob <- first iteration
obar fo <- second iteration
bar  <- third iterations
a <- fourth iteration
empty string == index error
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321