-1

When trying to replace '\' in python, the data changed and give me unknown letters.

i have tried string.replace, re.sub, regex_replace

a = '70\123456'
b = '70\123\456'

a = a.replace('\\','-')
b = b.replace('\\','-')

Expected Result:

a = '70-123456'
b = '70-123-456'

But The Actual Result is:

a = 70S456
b = 70SĮ

What is the problem and how to solve it?

Rakesh
  • 81,458
  • 17
  • 76
  • 113

1 Answers1

6

That's because \123 and \456 are special characters(octal). Try this:

a = r'70\123456'
b = r'70\123\456'

a = a.replace('\\','-')
b = b.replace('\\','-')

print(a)
print(b)
Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23