Change Based on your last comment, you will most likely want to switch from Unicode to some encoded representation. This is one way:
>>> s = '\n\t' >>> s.encode('unicode-escape') b'\\n\\t'
If you do not need screens, use system encoding, for example:
>>> s.encode('utf8') b'\n\t'
You can use this 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()
source share