I want to create a list of tuples, where I want:
- first element of tuple = index of alphabet
- second element of tuple = index of whitespace before the next alphabet
# String
input= "M i n d"
# List of tuple
output = [(0, 3), (4, 9), (10, 18), (19, 19)]
I was able to write this logic(with an error in the last tuple), but feel that there must be a smarter way of writing this. Any idea?
string = "M i n d"
coltuple = []
for a in string:
if a.isalpha() == True:
start = string.index(a)
next_string = string[(start + 1) :]
if next_string:
for b in next_string:
if b.isalpha() == True:
end = string.index(b) - 1
print("End:", end)
break
else:
end = len(string) - 1
coltuple += [(start, end)]
print(coltuple)