How to replace repeated instances of a character with a single instance of that character in python

前端 未结 11 1296
北海茫月
北海茫月 2020-12-31 00:29

I want to replace repeated instances of the \"*\" character within a string with a single instance of \"*\". For example if the string is \"*

11条回答
  •  隐瞒了意图╮
    2020-12-31 00:52

    how about a non regex way

    def squeeze(char,s):
        while char*2 in s:
            s=s.replace(char*2,char)
        return s
    print(squeeze("*" , "AB***abc**def**AA***k"))
    

    This returns AB*abc*def*AA*k

提交回复
热议问题