Removing backslashes from a string in Python


How do I remove all the backslashes from a string in Python?

This is not working for me:

result = result.replace("\\", result)

Do I need to treat result as a raw string?

7/1/2010 6:46:56 PM

Accepted Answer

Your code is saying to replace each instance of '\' with result. Have you tried changing it to result.replace("\\", "") ?

7/1/2010 6:49:20 PM

Use decode('string_escape'), for example:

result = stringwithbackslashes.decode('string_escape')

string_escape : Produce a string that is suitable as string literal in Python source code

or just:

result.replace("\\", "") 

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow