Let's have a variable string defined as:
string = "5+--+-+-+-+--+++---++-+-+-+-5"
What is the best way to replace all "++"
by "+"
, all "--"
by "+"
and all "-+"
and "+-"
by "-"
, to get:
string = "5+5"
I thought about:
from re import sub
while True:
if "-+" not in string and "+-" not in string and "++" not in string and "--" not in string:
break
string = sub("\++", "+", string).replace("--", "+").replace("+-", "-").replace("-+", "-")
Is it the best way to do it?