jason
jason

Reputation: 2106

remove special escape python

I have the string

a = 'ddd\ttt\nnn'

I want to remove the '\' from the string. and It will be

a = 'dddtttnnn'

how to do that in python since '\t' and '\n' has special meaning in python

Upvotes: 2

Views: 117

Answers (1)

dawg
dawg

Reputation: 103694

Assuming you want to remove \t and \n type characters (with those representing tab and newline in this case and remove the meaning of \ in the string in general) you can do:

>>> a = 'ddd\ttt\nnn'
>>> print a
ddd tt
nn
>>> repr(a)[1:-1].replace('\\','')
'dddtttnnn'
>>> print repr(a)[1:-1].replace('\\','')
dddtttnnn

If it is a raw string (i.e., the \ is not interpolated to a single character), you do not need the repr:

>>> a = r'ddd\ttt\nnn'
>>> a.replace('\\','')
'dddtttnnn'

Upvotes: 2

Related Questions