import re s = "Example String" replaced = re.sub('[ES]', 'a', s) print replaced # will print 'axample atring'
Here is what the above code is Doing:
1. The first argument is the regular expression.
2. The second argument is the replacement string.
3. The third argument is the original string.
4. The fourth argument is the maximum number of substitutions to be made.