title: pythonic way to replace two character in a string
body: <p>I have a string ,for example <code>s = &quot;-1+2-3&quot;</code> I want to replace all <code>-</code> to <code>+</code> and all <code>+</code> to <code>-</code>.</p>
  <p>what I expected is <code>s = +1-2+3</code> .So I can't just use <code>s.replace('-','+').replace('+','-')</code>,because it return <code>s=-1-2-3</code>,maybe a for loop can help. But I wonder if there is a pythonic way to do so?</p>
  <blockquote>
  <p>thanks for all solutions i did a simple test for all function below</p>
  </blockquote>
  <pre class="lang-py prettyprint-override"><code>def a(s=&quot;-1+2-3&quot;):
      s = re.sub('[+-]', lambda match: '-' if match[0] == '+' else '+', s)
      return s
  
  def b(s=&quot;-1+2-3&quot;):
      PLUS = '+'
      MINUS = '-'
      swap = {ord(PLUS): ord(MINUS), ord(MINUS): ord(PLUS)}
      s = s.translate(swap)
      return s
  
  def c(s=&quot;-1+2-3&quot;):
      replacements = {&quot;+&quot;: &quot;-&quot;, &quot;-&quot;: &quot;+&quot;}
  
      return ''.join([replacements[i] if i in replacements.keys() else i for i in s])
  
  
  def d(s=&quot;-1+2-3&quot;):
      return s.replace('+', '\x01').replace('-', '+').replace('\x01', '-')
  
  if __name__ == '__main__':
      a = timeit.timeit(a, number=100000) # a=0.20307550000000002
      b = timeit.timeit(b, number=100000) # b=0.08596850000000006
      c = timeit.timeit(c, number=100000) # c=0.12203799999999998
      d = timeit.timeit(d, number=100000) # d=0.033226100000000036
      print(f&quot;{a=}\n{b=}\n{c=}\n{d=}\n&quot;)
  </code></pre>