Removing backslashes from a string in Python
Question:
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?
Answers:
Your code is saying to replace each instance of ''
with result
. Have you tried changing it to result.replace("\", "")
?
result = result.replace("\", "")
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("\", "")
If you are interested in seeing the component words that are being separated by the ” character, use:
result.replace('\', ' ')
Try This:
result = result.replace("\", "")
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?
Your code is saying to replace each instance of ''
with result
. Have you tried changing it to result.replace("\", "")
?
result = result.replace("\", "")
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("\", "")
If you are interested in seeing the component words that are being separated by the ” character, use:
result.replace('\', ' ')
Try This:
result = result.replace("\", "")