remove backslash from string python

Solutions on MaxInterview for remove backslash from string python by the best coders in the world

showing results for - "remove backslash from string python"
Blake
03 Sep 2019
1# if the string is inputed by a user do this
2
3user_input = str(input("Enter a sentence here -- > ")) 
4# Let's say we inputed: "Thi\s i\s a sen\ten\ce wi\th bac\kslashes"
5user_input = user_input.replace('\\', '') 
6# we do double backslash because a singular backslash will cancel out whatever
7# Comes after it
8print(user_input) 
9# result: This is a sentence with backslashes
10
11# But if you give the variable "string" the value of Thi\s i\s a sen\ten\ce wi\th bac\kslashes:
12# Thi\s i\s a sen\ten\ce wi\th bac\kslashes 
13# And it gets printed as: 
14# Thi\s i\s a sen        en\ce wi        h bac\kslashes
15
16# You have to replace replace the "\"'s with "\\" 
17# So something like this:
18string = 'Thi\\s i\\s a sen\\ten\\ce wi\\th bac\\kslashes'
19print(string) 
20# The result will be:
21# Thi\s i\s a sen\ten\ce wi\th bac\kslashes 
22
23# and there you go, that's how you replace or get rid of
24# Backslashes