Skip to content

Commit faf05a1

Browse files
committed
Byte strings, raw strings; f-string stub
1 parent 86bf94b commit faf05a1

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

Doc/reference/lexical_analysis.rst

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,21 @@ Note that as in all lexical definitions, whitespace is significant.
643643
In particular, the prefix (if any) must be immediately followed by the starting
644644
quote.
645645

646+
.. index:: physical line, escape sequence, Standard C, C
647+
single: \ (backslash); escape sequence
648+
single: \\; escape sequence
649+
single: \a; escape sequence
650+
single: \b; escape sequence
651+
single: \f; escape sequence
652+
single: \n; escape sequence
653+
single: \r; escape sequence
654+
single: \t; escape sequence
655+
single: \v; escape sequence
656+
single: \x; escape sequence
657+
single: \N; escape sequence
658+
single: \u; escape sequence
659+
single: \U; escape sequence
660+
646661
.. _escape-sequences:
647662

648663
Escape sequences
@@ -842,8 +857,18 @@ Bytes literals
842857
:dfn:`Bytes literals` are always prefixed with ``'b'`` or ``'B'``; they produce an
843858
instance of the :class:`bytes` type instead of the :class:`str` type.
844859
They may only contain ASCII characters; bytes with a numeric value of 128
845-
or greater must be expressed with escape sequences.
846-
Similarly, a zero byte must be expressed using an escape sequence.
860+
or greater must be expressed with escape sequences (typically
861+
:ref:`string-escape-hex` or :ref:`string-escape-oct`):
862+
863+
.. code-block:: python
864+
865+
>>> b'\x89PNG\r\n\x1a\n'
866+
b'\x89PNG\r\n\x1a\n'
867+
>>> list(b'\x89PNG\r\n\x1a\n')
868+
[137, 80, 78, 71, 13, 10, 26, 10]
869+
870+
Similarly, a zero byte must be expressed using an escape sequence (typically
871+
``\0`` or ``\x00``).
847872

848873

849874
.. index::
@@ -860,7 +885,12 @@ or ``'R'``; such constructs are called :dfn:`raw string literals`
860885
and :dfn:`raw bytes literals` respectively and treat backslashes as
861886
literal characters.
862887
As a result, in raw string literals, :ref:`escape sequences <escape-sequences>`
863-
escapes are not treated specially.
888+
are not treated specially:
889+
890+
.. code-block:: python
891+
892+
>>> r'\d{4}-\d{2}-\d{2}'
893+
'\\d{4}-\\d{2}-\\d{2}'
864894
865895
Even in a raw literal, quotes can be escaped with a backslash, but the
866896
backslash remains in the result; for example, ``r"\""`` is a valid string
@@ -872,22 +902,6 @@ that a single backslash followed by a newline is interpreted as those two
872902
characters as part of the literal, *not* as a line continuation.
873903

874904

875-
.. index:: physical line, escape sequence, Standard C, C
876-
single: \ (backslash); escape sequence
877-
single: \\; escape sequence
878-
single: \a; escape sequence
879-
single: \b; escape sequence
880-
single: \f; escape sequence
881-
single: \n; escape sequence
882-
single: \r; escape sequence
883-
single: \t; escape sequence
884-
single: \v; escape sequence
885-
single: \x; escape sequence
886-
single: \N; escape sequence
887-
single: \u; escape sequence
888-
single: \U; escape sequence
889-
890-
891905
.. index::
892906
single: formatted string literal
893907
single: interpolated string literal
@@ -1067,6 +1081,19 @@ include expressions.
10671081
See also :pep:`498` for the proposal that added formatted string literals,
10681082
and :meth:`str.format`, which uses a related format string mechanism.
10691083

1084+
.. _t-strings:
1085+
.. _template-string-literals:
1086+
1087+
t-strings
1088+
---------
1089+
1090+
A :dfn:`template string literal` or :dfn:`t-string` is a string literal that
1091+
is prefixed with ``'t'`` or ``'T'``.
1092+
These strings have internal structure similar to :ref:`f-strings`,
1093+
but are evaluated as Template objects instead of strings.
1094+
1095+
.. versionadded:: 3.14
1096+
10701097

10711098
.. _numbers:
10721099

0 commit comments

Comments
 (0)