I answered a question earlier where the OP asked how he can remove backslashes from a string. This is how the backslashes looked like in the OP's string:
"I don\'t know why I don\'t have the right answer"
This was my answer:
a = "I don\'t know why I don\'t have the right answer"
b = a.strip("/")
print b
This removed the backslashes from the string but my answer was down-voted and I received a comment that said "there are so many things wrong with my answer that it's hard to count" I totally believe that my answer is probably wrong but I would like to know why so I can learn from it. However, that question was deleted by the author so I couldn't reply to the comment there to ask this question.
Well, there are no slashes, nor backslashes, in the string. The backslashes escape '
, although they don't have to because the string is delimited with ""
.
print("I don\'t know why I don\'t have the right answer")
print("I don't know why I don't have the right answer")
Produces:
I don't know why I don't have the right answer
I don't know why I don't have the right answer
Moreover, you are using wrong character and strip
only removes characters from the ends of the string:
Python 2.7.9 (default, Mar 1 2015, 12:57:24)
>>> print("///I don't know why ///I don't have the right answer///".strip("/"))
I don't know why ///I don't have the right answer
To put a backslash into a string you need to escape it too (or use raw string literals).
>>> print("\\I don't know why ///I don't have the right answer\\".strip("/"))
\I don't know why ///I don't have the right answer\
As you can see even though the backslashes were at the beginning and the end of the string they didn't get removed.
Finally, to answer the original question. One way is to use replace
method on string:
>>> print("\\I don't know why \\\I don't have the right answer\\".replace("\\",""))
I don't know why I don't have the right answer
Also, props for reaching out for a good answer after you screwed your own one =).
a = "I don\'t know why I don\'t have the right answer"
b = a.strip("/")
print b
/
) and backslash (\
) are not the same character. There are no slashes anywhere in the string, so what you're doing will have no effect.a
anyway; \'
in a non-raw string literal is just a '
character.strip
only removes "the leading and trailing characters". Since you're trying to remove a character in the middle of the string, it won't help.And maybe some meta-problems:
At any rate, that's not too many to count.
Then again, the comment didn't say it was too many to count, just hard to count. Some people have trouble counting to four three. Even Kings of the Britons have to be reminded by their clerics how to do it.
Let me describe the whole thing that is wrong
a = "I don\'t know why I don\'t have the right answer"
^ ^
Here as you can see both the back slashes are actually escaping the character literal '
which has a saved meaning.
Now coming to your code, what exactly does str.strip
do? From the docs:
Return a copy of the string with the leading and trailing characters removed
So you are not removing the back slashes but are deleting forward slashes from the ends of the string if any, when you write the piece of code as!
b = a.strip("/")
However you are getting no back slashes when you are displaying. This is because back slashes are used only for internal python representation and when you print them they will be interpreted as their escape characters and hence you will not get to see the back slashes. You can see the repr
output for a better view.
But the point to be noted is that, you need not have the back slashes at all as you are using "
to represent the string. So
a = "I don't know why I don't have the right answer"
is adequate enough!
This topic is covered far and wide in the official python documentation
>>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't"
The above snippet is taken directly from the documentation.
The \
are escaping the '
which when using double quotes is not needed. If you just print a
you would not see them either so b = a.strip("/")
is actually doing nothing.
In [7]: a = "I don\'t know why I don\'t have the right answer"
In [8]: a
Out[8]: "I don't know why I don't have the right answer"
In single quotes:
In [19]: a = 'I don\'t know why I don\'t have the right answer'
In [20]: a
Out[20]: "I don't know why I don't have the right answer"
If you were actually trying to strip an actual \
from a string you would use string.strip("\\")
or to replace/remove string.replace("\\","")
, \'s
are used to escape special characters:
The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.
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