2

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

6
  • Just escape the \ , as in \\ . Commented Aug 19, 2016 at 14:02
  • 1
    Does your starting string actually contain \t and \n (tabs and new lines)? Commented Aug 19, 2016 at 14:03
  • 3
    .replace("\t", "t").replace("\n", "n") Commented Aug 19, 2016 at 14:03
  • @jason> Your question is unclear at the moment. You must edit it to make it explicit: does your string contain the literal characters (d, d, d, \, t, t, t, \, n, n, n) or tabs and new lines, that is, (d, d, d, tab (\t), t, t, newline (\n), n, n) Commented Aug 19, 2016 at 14:20
  • Thanks for the suggest. my string is (d, d, d, \, t, t, t, \, n, n, n) Commented Aug 19, 2016 at 15:06

1 Answer 1

2

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' 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.