how to replace back slash character with empty string in python

Solution 1:

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

Solution 2:

The error is because you did not add a escape character to your '\', you should give \\ for backslash (\)

In [147]: foo = "a\c\d" # example string with backslashes

In [148]: foo 
Out[148]: 'a\\c\\d'

In [149]: foo.replace('\\', " ")
Out[149]: 'a c d'

In [150]: foo.replace('\\', "")
Out[150]: 'acd'

Solution 3:

Just to give you an explanation: the backslash \ has a special meaning in many languages. In Python, taking from the doc:

The backslash () character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

So, in order to replace \ in a string, you need to escape the backslash itself using "\\"

>>> "this is a \ I want to replace".replace("\\", "?")
'this is a ? I want to replace'