25

I am trying replace a backslash '\' in a string with the following code

string = "<P style='TEXT-INDENT'>\B7 </P>"

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

result:

------------------------------------------------------------
   File "<ipython console>", line 1
     result = string.replace("\",'')
                                     ^
SyntaxError: EOL while scanning string literal

Here i don't need the back slashes because actually i am parsing an xml file which has a tag in the above format, so if backslashes are there it is displaying invalid token during parsing

Can i know how to replace the backslashes with empty string in python

Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313

7 Answers7

40

We need to specify that we want to replace a string that contains a single backslash. We cannot write that as "\", because the backslash is escaping the intended closing double-quote. We also cannot use a raw string literal for this: r"\" does not work.

Instead, we simply escape the backslash using another backslash:

result = string.replace("\\","")
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
6

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'
mdoc-2011
  • 2,747
  • 4
  • 21
  • 43
avasal
  • 14,350
  • 4
  • 31
  • 47
4

In Python, as explained in the documentation:

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 with another backslash, thus:

>>> "this is a \ I want to replace".replace("\\", "?")
'this is a ? I want to replace'
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Pierre GM
  • 19,809
  • 3
  • 56
  • 67
  • Note that the `\ ` sequence in the string literal in the example becomes an actual backslash followed by a space. When Python scans a string literal and finds an unrecognized escape sequence, it leaves the backslash in the string. (Other programming languages might report an error, or silently remove the backslash.) – Karl Knechtel Aug 04 '22 at 20:45
2

Using regular expressions:

import re

new_string = re.sub("\\\\", "", old_string)

The trick here is that "\\\\" is a string literal describing a string containing two backslashes (each one is escaped), then the regex engine compiles that into a pattern that will match one backslash (doing a separate layer of unescaping).

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
0

Adding a solution if string='abcd\nop.png'

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

This above won't work as it'll give result='abcd\nop.png'.

Here if you see \n is a newline character. So we have to replace backslah char in raw string(as there '\n' won't be detected)

string.encode('unicode_escape')
result = string.replace("\\", "")
#result=abcdnop.png
valeriyan
  • 69
  • 1
  • 5
  • Someone who expects to "replace a backslash" in that string (which doesn't actually contain one) has a completely different question, driven by a different misconception. – Karl Knechtel Aug 04 '22 at 20:46
-1

You need to escape '\' with one extra backslash to compare actually with \.. So you should use '\'..

See Python Documentation - section 2.4 for all the escape sequences in Python.. And how you should handle them..

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
-1

It's August 2020.
Python 3.8.1
Pandas 1.1.0
At this point in time I used both the double \ backslash AND the r.

df.replace([r'\\'], [''], regex=True, inplace=True)

Cheers.

rickuls
  • 1
  • 2