How to string value of character count from input and desired output

How to string value of character count from input and desired output


HCL
Input:

data = ("aabbbbzzcma")

Output:

a3b4c1m1z2

Solve:

data = str("aabbbbzzcma")

count= {}

for ch in data:

    if ch not in count:

        count[ch]=1

    else:

        count[ch]+=1

output = ""

for ch,count in count.items():

    output+=ch+str(count)


print(output)

Comments