Let’s say I have this string 'foo1bar2xyz'
I know the indexes for the digits in it {'1': 3, '2': 7}
I want to form substrings of the parent string which don’t have the numbers. How would I get substrings of a string removing particular indexes?
Which in the above case which would be ['foo', 'bar', 'xyz']
Have tried this so far
def iterate_string(og_string, start, stop):
if start == 0:
return og_string[:stop]
else:
return og_string[start+1:stop]
def ret_string(S):
digit_dict = {c:i for i,c in enumerate(S) if c.isdigit()}
digit_positions = list(digit_dict.values())
# return digit_positions
substrings = []
start_index = 0
for position in digit_positions:
p = iterate_string(S, start_index, position)
substrings.append(p)
start_index = position
return substrings
print ret_string('foo1bar2xyz')
But this returns ['foo', 'bar']