Unicode raw string containing \u

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Oct 28 03:21:51 EDT 2007


On Sun, 28 Oct 2007 06:58:48 +0000, OKB (not okblacke) wrote:

> I'm trying to write a unicode raw string literal, and I seem to be
> running up against a conflict between the \uXXXX unicode character
> escape and the need to have a literal \u (i.e., backslash followed by a
> lowercase letter U) in the string.
> 
>     	If I do ur"\universe" I get a UnicodeDecodeError because (I think)
> it tries to interpret \universe as a Unicode escape.  But if I do
> ur"\\universe" I get a string that contains two backslashes followed by
> the word "universe".

That's because in a raw string, \\ means two backslashes.

>     	How can I specify a unicode raw string literal that contains a
> single backslash followed by the word "universe"?

The usual way.

>>> word = u'\\universe'
>>> len(word)
9
>>> word[0]
u'\\'
>>> word[1]
u'u'
>>> print word
\universe
>>> word
u'\\universe'


-- 
Steven.




More information about the Python-list mailing list