change string to unicode
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Fri Dec 19 10:35:53 EST 2008
On Fri, 19 Dec 2008 09:19:28 -0600, jyoung79 wrote:
> If I have a string like so:
>
> a = '\\u03B1'
>
> and I want to make it display a Greek alpha character, is there a way to
> convert it to unicode ('\u03B1')? I tried concatenating it like this:
>
> '\u' + '03B1'
>
> but that didn't work. I'm working in Python 3.0 and was curious if this
> could be done.
This is from Python 2.5:
>>> print unichr(0x03B1)
α
I don't have Python 3 here, but I guess that you would just use chr()
instead of unichr().
If you literally have to start with the actual string '\\u03B1' (that is,
backslash lowercase-U zero three uppercase-B one), then I'd do this:
>>> s = '\\u03B1'
>>> if s.startswith('\\u'):
... s = s[2:]
...
>>> print unichr(int(s, 16))
α
--
Steven
More information about the Python-list
mailing list