简体   繁体   中英

Python3 - last character of the string

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

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'
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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM