import rerx = r'[][._!"`\'#%&,:;<>=@{}~$()*+/\\?^|-]+'text = "My name is * and he is *"print( re.sub(rx, r'\\\g<0>', text) )
Standard input is empty
My name is \* and he is \*
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!