0
import re
pattern = re.compile(r"/")
a = "a/b"

I tried

re.sub(pattern, '\/', a) 
#(also, a.replace('/', '\/'))
#output
a\\/b

What I want is

a\/b
vb_rises
  • 1,847
  • 1
  • 9
  • 14

2 Answers2

1
a.replace('/', '\\/')

the first \ is an escape character, so you need to type it twice to have the real \.

User
  • 806
  • 1
  • 11
  • 28
1

You can use if it's not compulsory to use regex:

a = "a/b"
a=a.replace("/","\/")
print(a)
Brian Brix
  • 449
  • 4
  • 12