Create a backslash-escaped version of a string?

Aahz aahz at
Sat Feb 13 16:26:52 CET 2010

In article <aa6966f8-4dda-4e5b-ab7c-828faaff6ee3 at>,
boblatest  <boblatest at> wrote:
>I'd like to have control characters in a string to be converted to
>their backslash-escaped counterparts. I looked in the encoders section
>of the string module but couldn't find anything appropriate. I could
>write it myself but I'm sure something of the sort exists. The
>hypothetical method "c_escaped()" would work like this:
>>>> a="abc\rdef"
>>>> print a.c_escaped()

This is a little different from what you want, but just for the sake of

>>> print repr(a)[1:-1]
Aahz (aahz at           <*>

"At Resolver we've found it useful to short-circuit any doubt and just        
refer to comments in code as 'lies'. :-)"

More information about the Python-list mailing list