Python Tip: Escaping Strings to C-style encodings

Wai Yip Tung contributed a useful snippet in the Python Cookbook for escaping non printable characters in python to C-style equivalents. It uses an undocumented part of the standard python library. Very neat.

>>> a="ABC\nDEF"
>>> print a.encode('string_escape')
ABC\nDEF
>>>


About this entry