Replacing repeated consecutive characters in Python

后端 未结 5 655
抹茶落季
抹茶落季 2021-01-23 05:02

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?\'         


        
5条回答
  •  无人共我
    2021-01-23 05:54

    def dup_char_remover(input):
        output=""
        t=""
        for c in input:
            if t!=c:
                output = output + c
            t=c
        return output
    
    input = "hiiii how arrrre youuu"
    output=dup_char_remover(input)
    print(output)
    

    hi how are you

提交回复
热议问题