Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing backslashes from a string in Python [duplicate]

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?

like image 310
syker Avatar asked Jul 01 '10 18:07

syker


People also ask

How do you remove backslashes from string?

To remove all backslashes from a string:Call the replaceAll method, passing it a string containing 2 backslashes as the first parameter and an empty string as the second - str. replaceAll('\\', '') . The replaceAll method returns a new string with all of the matches replaced.

How do you get rid of backslashes in Python?

Using replace() Function to Remove Backslash from String in Python. The replace() can be utilized to remove an escape sequence or just a backslash in Python by simply substituting it with space in Python.

How do I remove a slash from text in Python?

There are two popular ways to remove a trailing slash from a string in Python. The most common approach is to use the . rstrip() string method which will remove all consecutive trailing slashes at the end of a string. If you only need to remove one trailing slash the string slice operator can be another alternative.

How do you replace two backslash in Python?

Just use . replace() twice! first operation creates ** for every \ and second operation escapes the first slash, replacing ** with a single \ .


1 Answers

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

like image 122
fbstj Avatar answered Oct 11 '22 20:10

fbstj