You can use groupby from itertools module :
s = "ABBBBCCCCCCCCAB"
from itertools import groupby
expected = ''.join([str(len(list(v)))+k for k,v in groupby(s)])
Output :
'1A4B8C1A1B'
groupby(s) returns a itertools.groupby object. A list comprehension on this object like [(k,list(v)) for k,v in groupby(s)] returns us this in ordered way :
[('A', ['A']), ('B', ['B', 'B', 'B', 'B']), ('C', ['C', 'C', 'C', 'C', 'C', 'C', 'C', 'C']), ('A', ['A']), ('B', ['B'])]
We can just count the number of sub-items in the second-item of the tuple and add its string format before the first item of the tuple and join all of them.
Update :
You are trying to change the iteration index in the loop by doing char=char+1 but it doesn't change the iteration index i.e. the loop doesn't pass for the next 2 or 3 or 4 iterations. Add these two print lines in your code and you would see that the char variable you're trying to increase while looping is not simply the iteration index :
...
for char in range(0,len(message)-1,1):
print('\tchar at first line : ', char, 'char id now : ', id(char))
count=1
while(message[char]==message[char+1]):
count=count+1
char=char+1
print('char now : ', char, 'char id now : ', id(char))
...
It should output something like :
char at first line : 1 char id now : 11197408
char now : 2 char id now : 11197440
char now : 3 char id now : 11197472
char now : 4 char id now : 11197504
See, how the id of each time char got changed.