I have a string that is a sentence like I don't want it, there'll be others
So the text looks like this I don\'t want it, there\'ll be other
for some reason a \
comes with the text next to the '
. It was read in from another source. I want to remove it, but can't. I've tried.
sentence.replace("\'","'")
sentence.replace(r"\'","'")
sentence.replace("\\","")
sentence.replace(r"\\","")
sentence.replace(r"\\\\","")
I know the \
is to escape something, so not sure how to do it with the quotes
The \
is just there to escape the '
character. It is only visible in the representation (repr
) of the string, it's not actually a character in the string. See the following demo
>>> repr("I don't want it, there'll be others")
'"I don\'t want it, there\'ll be others"'
>>> print("I don't want it, there'll be others")
I don't want it, there'll be others
Try to use:
sentence.replace("\\", "")
You need two backslashes because first of them act as escape symbol, and second is symbol that you need to replace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With