SyntaxFix
Write A Post
Hire A Developer
Questions
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.