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,689
  • 1
  • 7
  • 13

2 Answers2

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

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

User
  • 719
  • 11
  • 23
1

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

a = "a/b"
a=a.replace("/","\/")
print(a)
Brian Brix
  • 364
  • 2
  • 11