lst = "aabbaccc"

ans = ""
c = 1

for n in range(1, len(lst)):
    if lst[n] == lst[n - 1]:
        c += 1
    else:
        if c > 1:
            ans += str(c)
        ans += lst[n - 1]
        c = 1

if c > 1:
    ans += str(c)
ans += lst[-1]

print(ans, len(ans))

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: