Replace escape character in python

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

string = "

\B7

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

result:

------------------------------------------------------------
   File "", 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

asked Sep 27, 2012 at 9:18

0

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("\\","")

answered Sep 27, 2012 at 9:20

0

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,5174 gold badges20 silver badges40 bronze badges

answered Sep 27, 2012 at 9:19

avasalavasal

13.8k4 gold badges30 silver badges47 bronze badges

1

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'

answered Sep 27, 2012 at 9:27

Pierre GMPierre GM

19k3 gold badges54 silver badges65 bronze badges

1

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).

answered Aug 5, 2020 at 21:09

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

answered Jul 20, 2020 at 12:34

Replace escape character in python

valeriyanvaleriyan

591 silver badge5 bronze badges

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..

answered Sep 27, 2012 at 9:28

Rohit JainRohit Jain

205k43 gold badges399 silver badges515 bronze badges

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.

answered Aug 15, 2020 at 1:10

How do I remove an escape character in Python?

Practical Data Science using Python You can use regexes to remove the ANSI escape sequences from a string in Python. Simply substitute the escape sequences with an empty string using re. sub(). The regex you can use for removing ANSI escape sequences is: '(\x9B|\x1B\[)[0-?]

How do you replace a character in Python?

Syntax of replace().
old – old substring you want to replace..
new – new substring which would replace the old substring..
count – (Optional ) the number of times you want to replace the old substring with the new substring..

How do you escape special characters in a string in Python?

To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.

How do you replace a backslash character with an empty string in Python?

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..