import re input_text = "abccbcbbb"; k=3 input_text = re.sub(fr"(.)\1{{{k-1}}}", "", input_text) print(input_text)