Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2f837f

Browse files
committed
Document the fact that '\U' and '\u' escapes are not treated specially in 3.0 (see issue 2541)
1 parent a288fae commit a2f837f

3 files changed

Lines changed: 11 additions & 11 deletions

File tree

Doc/reference/lexical_analysis.rst

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,9 @@ characters that otherwise have a special meaning, such as newline, backslash
423423
itself, or the quote character.
424424

425425
String literals may optionally be prefixed with a letter ``'r'`` or ``'R'``;
426-
such strings are called :dfn:`raw strings` and use different rules for
427-
interpreting backslash escape sequences.
426+
such strings are called :dfn:`raw strings` and treat backslashes as literal
427+
characters. As a result, ``'\U'`` and ``'\u'`` escapes in raw strings are not
428+
treated specially.
428429

429430
Bytes literals are always prefixed with ``'b'`` or ``'B'``; they produce an
430431
instance of the :class:`bytes` type instead of the :class:`str` type. They
@@ -520,15 +521,6 @@ is more easily recognized as broken.) It is also important to note that the
520521
escape sequences only recognized in string literals fall into the category of
521522
unrecognized escapes for bytes literals.
522523

523-
When an ``'r'`` or ``'R'`` prefix is used in a string literal, then the
524-
``\uXXXX`` and ``\UXXXXXXXX`` escape sequences are processed while *all other
525-
backslashes are left in the string*. For example, the string literal
526-
``r"\u0062\n"`` consists of three Unicode characters: 'LATIN SMALL LETTER B',
527-
'REVERSE SOLIDUS', and 'LATIN SMALL LETTER N'. Backslashes can be escaped with a
528-
preceding backslash; however, both remain in the string. As a result,
529-
``\uXXXX`` escape sequences are only recognized when there is an odd number of
530-
backslashes.
531-
532524
Even in a raw string, string quotes can be escaped with a backslash, but the
533525
backslash remains in the string; for example, ``r"\""`` is a valid string
534526
literal consisting of two characters: a backslash and a double quote; ``r"\"``

Doc/whatsnew/3.0.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ Strings and Bytes
167167
explicitly convert between them, using the :meth:`str.encode` (str -> bytes)
168168
or :meth:`bytes.decode` (bytes -> str) methods.
169169

170+
* All backslashes in raw strings are interpreted literally. This means that
171+
Unicode escapes are not treated specially.
172+
170173
.. XXX add bytearray
171174
172175
* PEP 3112: Bytes literals, e.g. ``b"abc"``, create :class:`bytes` instances.
@@ -183,6 +186,8 @@ Strings and Bytes
183186
* The :mod:`StringIO` and :mod:`cStringIO` modules are gone. Instead, import
184187
:class:`io.StringIO` or :class:`io.BytesIO`.
185188

189+
* ``'\U'`` and ``'\u'`` escapes in raw strings are not treated specially.
190+
186191

187192
PEP 3101: A New Approach to String Formatting
188193
=============================================

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Core and Builtins
2626
through as unmodified as possible; as a consequence, the C API
2727
related to command line arguments was changed to use wchar_t.
2828

29+
- All backslashes in raw strings are interpreted literally. This means that
30+
'\u' and '\U' escapes are not treated specially.
31+
2932
Extension Modules
3033
-----------------
3134

0 commit comments

Comments
 (0)