2

Just wonder how to convert a unicode string like u'é' to its unicode character code u'\xe9'?

Martin G
  • 17,357
  • 9
  • 82
  • 98
boativan66
  • 23
  • 3

3 Answers3

3

You can use Python's repr() function:

>>> unicode_char = u'é'
>>> repr(unicode_char)
"u'\\xe9'"
Andrew
  • 12,821
  • 2
  • 26
  • 18
1

ord will give you the numeric value, but you'll have to convert it into hex:

>>> ord(u'é')
233
Peter Milley
  • 2,768
  • 19
  • 18
1

u'é' and u'\xe9' are exactly the same, they are just different representations:

>>> u'é' == u'\xe9'
True
  • The expression evaluate to True in python but False in ipython. Any reason why that happens? Thank you! – boativan66 Aug 17 '10 at 20:21
  • @boativan66: you might have converted it to `u'e\u0301'` during a copy-paste. Try `unicodedata.normalize('NFC', u'é') == u'\xe9'` – jfs Oct 14 '15 at 06:14