我建议
itertools.groupby
然后根据需要格式化
from itertools import groupby
# groupby("assdggg")
# {'a': ['a'], 's': ['s', 's'], 'd': ['d'], 'g': ['g', 'g', 'g']}
result = ""
for k, v in groupby("assdggg"):
count = len(list(v))
result += (str(count) if count > 1 else "") + k
print(result) # a2sd3g