Unicode raw string containing \u

O

OKB (not okblacke)

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".

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

--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
 
S

Steven D'Aprano

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'
 
O

OKB (not okblacke)

Steven said:
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'

That doesn't answer my question, since I asked for a unicode RAW
string literal. Is this not possible? (I was able to get what I want
using ur"\u005Cuniverse", although this is not totally ideal.)

--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
 
?

=?ISO-8859-15?Q?=22Martin_v=2E_L=F6wis=22?=

That doesn't answer my question, since I asked for a unicode RAW
string literal. Is this not possible? (I was able to get what I want
using ur"\u005Cuniverse", although this is not totally ideal.)

It's a design flaw in Unicode raw string literals that they still
interpret \u escapes. And yes, your notation is one way to get what
you want; another is u"\\"+r"universe", although I'm unsure whether
that meets your requirements.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top