Python count group letters in a string

P

Say we have a string like “abaabbbccccaaac”. We want to count same group of letters in a string and display like “a1b1a2b3c4a3c1”

The code is given below:

a = "abaabbbccccaaac"
o = ""
while len(a) > 0:
    count = 0
    i = 0
    ch = a[0]

    while i < len(a) and a[i] == ch:
        count += 1
        i += 1
    a = a[count:]
    o += ch+str(i)
    
print(o)

About the author

Imran Pollob

Learn => Practice => Implement => Repeat

Add comment

Categories

Imran Pollob

Learn => Practice => Implement => Repeat

Get in touch