Python "string_escape" vs "unicode_escape"
Posted
by Mike Boers
on Stack Overflow
See other posts from Stack Overflow
or by Mike Boers
Published on 2010-06-03T19:18:47Z
Indexed on
2010/06/03
19:34 UTC
Read the original article
Hit count: 140
According to the docs, the builtin string encoding string_escape
:
Produce[s] a string that is suitable as string literal in Python source code
...while the unicode_escape
:
Produce[s] a string that is suitable as Unicode literal in Python source code
So, they should have roughly the same behaviour. BUT, they appear to treat single quotes differently:
>>> print """before '" \0 after""".encode('string-escape')
before \'" \x00 after
>>> print """before '" \0 after""".encode('unicode-escape')
before '" \x00 after
The string_escape
escapes the single quote while the Unicode one does not. Is it safe to assume that I can simply:
>>> escaped = my_string.encode('unicode-escape').replace("'", "\\'")
...and get the expected behaviour?
© Stack Overflow or respective owner