How can I efficiently replace substrings in python, where one substring may be part of another? For example:
>>> "||||".replace("||","|X|")
'|X||X|'
# What I want: |X|X|X|
Certainly I could keep repeating the replace and until there are no more instances of || in the string, but surely there's a better/faster way?
"||||".replace("|","|X") + '|'so just this then?|each would work.'aaaa'.replace('aaa', 'pony')? There are two overlapping instances of'aaa'in the input, but it doesn't really make sense to replace both of them."||||".replace("||","|X|X").strip('X')