How do I coalesce a sequence of identical characters into just one?

import re

astr='My---sun--is------very-big---.'

print(re.sub('-+','-',astr))
# My-sun-is-very-big-.

If you want to replace any run of consecutive characters, you can use

>>> import re
>>> a = "AA---BC++++DDDD-EE$$$$FF"
>>> print(re.sub(r"(.)\1+",r"\1",a))
A-BC+D-E$F

If you only want to coalesce non-word-characters, use

>>> print(re.sub(r"(\W)\1+",r"\1",a))
AA-BC+DDDD-EE$FF

If it's really just hyphens, I recommend unutbu's solution.


If you really only want to coalesce hyphens, use the other suggestions. Otherwise you can write your own function, something like this:

>>> def coalesce(x):
...     n = []
...     for c in x:
...         if not n or c != n[-1]:
...             n.append(c)
...     return ''.join(n)
...
>>> coalesce('My---sun--is------very-big---.')
'My-sun-is-very-big-.'
>>> coalesce('aaabbbccc')
'abc'