0

I need to remove backslash ('\') from string in Python.

str2 = str1.replace('\', '') isn`t working.

How can i do it?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Egor Demeshko
  • 33
  • 1
  • 4

3 Answers3

4

This is due to \ being the escape sequence character in python so you have to do \\

str2 = str1.replace('\\', '')  
Sean Powell
  • 564
  • 4
  • 20
-2

There are 2 ways you can do that:

You have to escape the \:

str2 = str1.replace('\\', '') 

or using raw strings:

str2 = str1.replace(r'\', '') 
luigibertaco
  • 1,112
  • 5
  • 21
-2

Little tricky but it works

str2 = str1.replace("\ ".replace(" ", ""), "")
Rizquuula
  • 578
  • 5
  • 15