Embedding a literal "\u" in a unicode raw string.

Thinker thinker at branda.to
Mon Feb 25 07:58:04 EST 2008


Romano Giannetti wrote:
> Hi,
>
> while writing some LaTeX preprocessing code, I stumbled into this
> problem: (I have a -*- coding: utf-8 -*- line, obviously)
>
> s = ur"añado $\uparrow$"
>
> Which gave an error because the \u escape is interpreted in raw
> unicode strings, too. So I found that the only way to solve this is
>  to write:
>
> s = unicode(r"añado $\uparrow$", "utf-8")
>
> or
>
> s = ur"añado $\u005cuparrow$"
>
> The second one is too ugly to live, while the first is at least
> acceptable; but looking around the Python 3.0 doc, I saw that the
> first one will fail, too.
>
> Am I doing something wrong here or there is another solution for
> this?
>
> Romano
>
The backslash '\' is a meta-char that escapes the string.  You can
escape the char as following string
u"....\\u....'
insert another '\' before it.





More information about the Python-list mailing list