I want to delete all characters after the third character, for example, for example.
I found this code online and it works, but I have problems learning how it works and wanted to ask so that I can fully understand it.
def indexList(s, item, i=0):
"""
Return an index list of all occurrances of 'item' in string/list 's'.
Optional start search position 'i'
"""
i_list = []
while True:
try:
i = s.index(item, i)
i_list.append(i)
i += 1
except:
break
return i_list
def strip_chrs(s, subs):
for i in range(indexList(s, subs)[-1], len(s)):
if s[i+1].isalpha():
return data[:i+1]
data = '115Z2113-3-777-55789ABC7777'
print strip_chrs(data, '-')
Here are my questions while True: string, what is the truth? Also except: except for what? and why is the gap encoded here?
Thanks in advance!
source
share