Getting unicode escape sequence from unicode character?

Carl Banks pavlovevidence at gmail.com
Wed Dec 27 17:27:32 EST 2006


Kenneth McDonald wrote:
> Given a Python unicode character (string of length one), how would I
> find out the \uXXXX escape sequence for it? This isn't obvious from the
> docs I've been looking through.

You can use the ord builtin, or the encode method with
"unicode_escape":

>>> a = u'\u1234'
>>> a
u'\u1234'
>>> print a
ሴ
>>> ord(a)
4660
>>> hex(ord(a))
'0x1234'
>>> a.encode('unicode_escape')
'\\u1234'


Carl Banks




More information about the Python-list mailing list