7

Say I have a string in Python 3.2 like this:

'\n'

When I print() it to the console, it shows as a new line, obviously. What I want is to be able to print it literally as a backslash followed by an n. Further, I need to do this for all escaped characters, such as \t. So I'm looking for a function unescape() that, for the general case, would work as follows:

>>> s = '\n\t'
>>> print(unescape(s)) 
'\\n\\t'

Is this possible in Python without constructing a dictionary of escaped characters to their literal replacements?

(In case anyone is interested, the reason I am doing this is because I need to pass the string to an external program on the command line. This program understands all the standard escape sequences.)

Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158
  • When you say "one the command line" I assume you actually mean via stdout, in which case the program you are passing the data to should consume the `\n`,etc... without any problems. That is, if you printed the data after it was passed into this second program, you would see the `\n` would still be there. Unless you really do want to escape it. But check first, if your just removing the extra `\` in the second program, you're probably doing it wrong. – Endophage Feb 18 '12 at 08:07
  • I have a command line program called myutility. It expects to be called like this: `myutility -i \n` If I don't unescape the string then it won't ever see the \n being passed in to it - it will assume -i to be an empty argument. – Mike Chamberlain Feb 18 '12 at 08:14
  • Related question, [Escape special characters in a Python string - Stack Overflow](https://stackoverflow.com/questions/4202538/escape-special-characters-in-a-python-string) – user202729 Dec 23 '22 at 08:40

2 Answers2

13

To prevent special treatment of \ in a literal string you could use r prefix:

s = r'\n'
print(s)
# -> \n

If you have a string that contains a newline symbol (ord(s) == 10) and you would like to convert it to a form suitable as a Python literal:

s = '\n'
s = s.encode('unicode-escape').decode()
print(s)
# -> \n
jfs
  • 399,953
  • 195
  • 994
  • 1,670
5

Edit: Based on your last remark, you likely want to get from Unicode to some encoded representation. This is one way:

>>> s = '\n\t'
>>> s.encode('unicode-escape')
b'\\n\\t'

If you don't need them to be escaped then use your system encoding, e.g.:

>>> s.encode('utf8')
b'\n\t'

You could use that in a subprocess:

import subprocess
proc = subprocess.Popen([ 'myutility', '-i', s.encode('utf8') ], 
                        stdout=subprocess.PIPE, stdin=subprocess.PIPE, 
                        stderr=subprocess.STDOUT)
stdout,stderr = proc.communicate()
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • 2
    Although `repr()` is really attractive, this _might_ not be exactly what is intended, as this will actually output a string containing the single quotes, so you'll end up with something like `"'\\n\\r'"`. – voithos Feb 18 '12 at 08:07
  • My Python evaluates that as "'\\n\\t'" I guess I can just strip the first and last characters...? – Mike Chamberlain Feb 18 '12 at 08:14
  • 1
    `repr('\n\t')` returns `"'\\n\\t'"` – Oleh Prypin Feb 18 '12 at 08:22
  • Remark: `unicode-escape`, unlike `repr`, will also escape characters outside the ASCII range e.g. `⊕` becomes `\u2295`. – user202729 Dec 23 '22 at 08:39