I want to replace repeated instances of the "*"
character within a string with a single instance of "*"
. For example if the string is "***abc**de*fg******h"
, I want it to get converted to "*abc*de*fg*h"
.
I'm pretty new to python (and programming in general) and tried to use regular expressions and string.replace() like:
import re
pattern = "***abc**de*fg******h"
pattern.replace("*"*, "*")
where *
is supposed to replace all instances of the "*" character. But I got: SyntaxError: unexpected character after line continuation character.
I also tried to manipulate it with a for loop like:
def convertString(pattern):
for i in range(len(pattern)-1):
if(pattern[i] == pattern[i+1]):
pattern2 = pattern[i]
return pattern2
but this has the error where it only prints "*" because pattern2 = pattern[i] constantly redefines what pattern2 is...
Any help would be appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…