String to unicode - duplicating by function the effect of u prefix

Peter Otten __peter__ at web.de
Thu Jun 18 09:08:33 CEST 2009


CiTro wrote:

> I'm looking for a way to convert a string to it's unicode "brother".
> 
> This means:
> 
> stringOne = "\u0026"
> stringTwo = u"\u0026"
> 
> print unicode(stringOne) == stringTwo
> 
> The result is false. What function should I use, to duplicate the
> effect of the "u" prefix ?

"\u..." has a special meaning only in unicode, not string literals:

>>> s = "\u0026"
>>> len(s)
6
>>> s.decode("unicode-escape")
u'&'
>>> _ == u"\u0026"
True

Peter




More information about the Python-list mailing list