Вы можете перебирать строку, сохраняя текущий счетчик, и создавать свою строку по ходу движения
s = 'aaaaggggtt'
res = ''
counter = 1
#Iterate over the string
for idx in range(len(s)-1):
#If the character changes
if s[idx] != s[idx+1]:
#Append last character and counter, and reset it
res += s[idx]+str(counter)
counter = 1
else:
#Else increment the counter
counter+=1
#Append the last character and it's counter
res += s[-1]+str(counter)
print(res)
Или вы можете подойти к этому, используя itertools.groupby
from itertools import groupby
s = 'aaaaggggtt'
#Count numbers and associated length in a list
res = ['{}{}'.format(model, len(list(group))) for model, group in groupby(s)]
#Convert list to string
res = ''.join(res)
print(res)
Выход будет
a4g4t2