How to remove backslash from string in Python?

Question:

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

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

How can i do it?

Asked By: Egor Demeshko

||

Answers:

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

str2 = str1.replace('\', '')  
Answered By: Sean Powell

There are 2 ways you can do that:

You have to escape the :

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

or using raw strings:

str2 = str1.replace(r'', '') 
Answered By: luigibertaco

Little tricky but it works

str2 = str1.replace(" ".replace(" ", ""), "")
Answered By: Rizquuula
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.