Skip to content
Advertisement

Tag: str-replace

pythonic way to replace two character in a string

I have a string ,for example s = “-1+2-3” I want to replace all – to + and all + to -. what I expected is s = +1-2+3 .So I can’t just use s.replace(‘-‘,’+’).replace(‘+’,’-‘),because it return s=-1-2-3,maybe a for loop can help. But I wonder if there is a pythonic way to do so? thanks for all solutions i

Advertisement