string remove backslash chars python code example
Example: remove backslash from string python
# if the string is inputed by a user do this
user_input = str(input("Enter a sentence here -- > "))
# Let's say we inputed: "Thi\s i\s a sen\ten\ce wi\th bac\kslashes"
user_input = user_input.replace('\\', '')
# we do double backslash because a singular backslash will cancel out whatever
# Comes after it
print(user_input)
# result: This is a sentence with backslashes
# But if you give the variable "string" the value of Thi\s i\s a sen\ten\ce wi\th bac\kslashes:
# Thi\s i\s a sen\ten\ce wi\th bac\kslashes
# And it gets printed as:
# Thi\s i\s a sen en\ce wi h bac\kslashes
# You have to replace replace the "\"'s with "\\"
# So something like this:
string = 'Thi\\s i\\s a sen\\ten\\ce wi\\th bac\\kslashes'
print(string)
# The result will be:
# Thi\s i\s a sen\ten\ce wi\th bac\kslashes
# and there you go, that's how you replace or get rid of
# Backslashes