Stupid Python Tricks – Unescape a String

Ever been given a pre-escaped string like ‘\\r\\n’ and wanted the actual non-escaped version?

Try this:

>>> ‘\\\\\\\\’.decode(’string_escape’)
‘\\\\’

I just found it today. Pretty cool, eh.

2 Responses to “Stupid Python Tricks – Unescape a String”

  1. Anonymous says:

    The given example cannot be used at an interactive prompt. You need '\\\\' or r'\\' and the output would be '\\' (that is, a single backslash escaped for the repr).

  2. @Anonymous, Fixed it. Thanks.