In [1]:
s = 'one two one two one'
In [2]:
print(s.replace('one', 'two').replace('two', 'one'))
In [3]:
print(s.replace('one', 'X').replace('two', 'one').replace('X', 'two'))
In [4]:
def swap_str(s_org, s1, s2, temp='*q@w-e~r^'):
return s_org.replace(s1, temp).replace(s2, s1).replace(temp, s2)
In [5]:
print(swap_str(s, 'one', 'two'))
In [6]:
print(s.replace('o', 't').replace('t', 'o'))
In [7]:
print(s.translate(str.maketrans({'o': 't', 't': 'o'})))
In [8]:
print(s.translate(str.maketrans('ot', 'to')))