I need to make a function that replaces repeated, consecutive characters with a single character, for example:
\'hiiii how are you??\' -> \'hi how are you?\'
Using a simple iteration.
Demo:
def cleanText(val):
result = []
for i in val:
if not result:
result.append(i)
else:
if result[-1] != i:
result.append(i)
return "".join(result)
s = ['hiiii how are you??', 'aahhhhhhhhhh whyyyyyy', 'foo', 'oook. thesse aree enoughh examplles.']
for i in s:
print(cleanText(i))
Output:
hi how are you?
ah why
fo
ok. these are enough examples.