def encode(s):
f = s[0]
count=1
tmp=''
for i in range(1,len(s)):
if f == s[i]:
count += 1
else:
tmp='%s%s%s'%(tmp,count,f)
f=s[i]
count=1
if count==1:
count=''
tmp='%s%s%s'%(tmp,count,f)
return tmp
s = 'aabbbccccd'
print(encode(s))