more on unescaping escapes

Rhodri James rhodri at
Mon Feb 23 19:32:18 EST 2009

On Mon, 23 Feb 2009 22:05:42 -0000, bvdp <bob at> wrote:

> So, we think something is working and send of a bug fix to our client :)
> I'm not sure I understand this at all and wonder if there is bug?
>  >>> a="c:\\Program\x20Files\\test"
>  >>> a
> 'c:\\Program Files\\test'
> so far, so good.
>  >>> a.decode("string-escape")
> 'c:\\Program Files\test'
> Umm, not so good? The \\ before the P is okay, but the \\t is change to  
> \t

Well yes, that's what you asked it to do.  The "string-escape" decoder
reads the string and replaces escape sequences with the corresponding
characters.  Bear in mind that it's the string as it really is that is
being operated on, not the representation of it that you displayed
above.  In other words:

b = a.decode("string-escape")

is equivalent to:

b = "C:\Program Files\test"

"\P" isn't a valid escape sequence, so it doesn't get replaced.  "\t"
represents a tab, so it does.

Rhodri James *-* Wildebeeste Herder to the Masses

More information about the Python-list mailing list