Reputation: 238
How could I remove the backslash before a character in Python, such as ":" or "!"?
Edit: Let's say I got this line : "This is a line of text\!"
. I want to get just "This is a line of text!"
, without the backslash.
Upvotes: 1
Views: 2350
Reputation: 25331
If you want to remove all backslashes, a simpler way is:
print "This is a line of text\!".replace('\\','')
Note that you have to escape the backslash, and that you may remove backslashes that are used to escape other characters.
Upvotes: 0
Reputation: 1566
You can use regex here:
>>> import re
>>> s = "This is a line of text\!, hello\: It\"s"
>>> re.sub(r'\\([!:])', r'\1', s)
'This is a line of text!, hello: It"s'
Upvotes: 3