I need to replace some characters as follows: & ➔ \\&, # ➔ \\#, ...
&
\\&
#
\\#
I coded as follows, but I guess there
Are you always going to prepend a backslash? If so, try
import re rx = re.compile('([])') # ^^ fill in the characters here. strs = rx.sub('\\\\\\1', strs)
It may not be the most efficient method but I think it is the easiest.