fork download
  1. import re
  2. rx = r'[][._!"`\'#%&,:;<>=@{}~$()*+/\\?^|-]+'
  3. text = "My name is * and he is *"
  4. print( re.sub(rx, r'\\\g<0>', text) )
  5.  
  6.  
Success #stdin #stdout 0.03s 9612KB
stdin
Standard input is empty
stdout
My name is \* and he is \*