Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing backslashes from string

Tags:

python

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.

like image 389
Joe T. Boka Avatar asked May 11 '15 00:05

Joe T. Boka


4 Answers

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 =).

like image 156
luk32 Avatar answered Oct 16 '22 04:10

luk32


a = "I don\'t know why I don\'t have the right answer"
b = a.strip("/")
print b
  1. Slash (/) and backslash (\) are not the same character. There are no slashes anywhere in the string, so what you're doing will have no effect.
  2. Even if you used a backslash, there are no backslashes in a anyway; \' in a non-raw string literal is just a ' character.
  3. 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:

  1. The OP's example string actually had just backslashes, not backslash-escaped apostrophes, even though his question title said the latter. So you were solving a different problem. Which may be more the OP's fault than yours, but it's still not a good answer.
  2. The OP's code already did what you were trying to do. He obviously had some other problem that he couldn't explain. (Given that he stopped answering comments and deleted his question, my first bet would be that it was a silly typo somewhere…) So, even if you wrote your code correctly, it wouldn't have been that helpful.

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.

like image 27
abarnert Avatar answered Oct 16 '22 05:10

abarnert


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.

like image 35
Bhargav Rao Avatar answered Oct 16 '22 05:10

Bhargav Rao


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.

like image 23
Padraic Cunningham Avatar answered Oct 16 '22 04:10

Padraic Cunningham