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

前端 未结 11 1443
北海茫月
北海茫月 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:41

    text = "aaaaaaaaaabbbbbbbbbbcccccccffffdffffdaaaaaa"

    result = " "

    for char in text:

    if len(result) > 0 and result[-1] == char:
        continue
    else:
        result += char
    

    print(result) # abcda

提交回复
热议问题