Drake Drake - 7 months ago 59
Python Question

Convert u'\xe0' to '\u00E0' in Python 2.x?

In Python 2.x, how can I convert an unicode string (ex,

) to a string (here I need it to be

To make it clearer. I do like to have
, a string with length of 6. That is,
is treated as 2 chars instead of one escaped char.


\u doesn't exist as a string escape sequence in Python 2.

You might mean a JSON-encoded string:

>>> s = u'\xe0'
>>> import json
>>> json.dumps(s)

or a UTF-16 (big-endian)-encoded string:

>>> s.encode("utf-16-be")

but your original request is not fulfillable.

As an aside, note that u'\u00e0' is identical to u'\xe0', but '\u00e0' doesn't exist:

>>> u'\u00e0'