s = "aaabbb"
count = 1
answer = ""

for i in range(1, len(s)):
    if s[i] == s[i-1]:
        count += 1
    else:
        answer += s[i-1] + str(count)
        count = 1

answer += s[-1] + str(count)

if len(answer) < len(s):
    print(answer)
else:
    print(s)

Embed on website

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