3

I'm in Python 2.7.

test = r'\U' 

gives:

SyntaxError: (unicode error) 'rawunicodeescape' codec can't decode bytes in position 0-1: truncated \uXXXX

I thought raw strings were supposed to ignore backslashes. No?

I want a string that actually contains '\' and 'U'.

In case it matters, I'm also using:

from __future__ import (absolute_import, division, print_function, unicode_literals)
nerdfever.com
  • 1,652
  • 1
  • 20
  • 41

1 Answers1

3

Python 2 raw unicode strings have a bizarre quirk where they still process \u and \U sequences:

When an 'r' or 'R' prefix is used in conjunction with a 'u' or 'U' prefix, then the \uXXXX and \UXXXXXXXX escape sequences are processed while all other backslashes are left in the string.

The docs describe the behavior of combining the r and u prefixes, but I believe this also applies to combining the r prefix and unicode_literals.

user2357112
  • 260,549
  • 28
  • 431
  • 505