@@ -277,7 +277,15 @@ Whitespace between tokens
277277
278278Except at the beginning of a logical line or in string literals, the whitespace
279279characters space, tab and formfeed can be used interchangeably to separate
280- tokens. Whitespace is needed between two tokens only if their concatenation
280+ tokens:
281+
282+ .. grammar-snippet ::
283+ :group: python-grammar
284+
285+ whitespace: ' ' | tab | formfeed
286+
287+
288+ Whitespace is needed between two tokens only if their concatenation
281289could otherwise be interpreted as a different token. For example, ``ab `` is one
282290token, but ``a b `` is two tokens. However, ``+a `` and ``+ a `` both produce
283291two tokens, ``+ `` and ``a ``, as ``+a `` is not a valid token.
@@ -921,24 +929,60 @@ f-strings
921929.. versionadded :: 3.6
922930
923931A :dfn: `formatted string literal ` or :dfn: `f-string ` is a string literal
924- that is prefixed with ``'f' `` or ``'F' ``. These strings may contain
925- replacement fields, which are expressions delimited by curly braces ``{} ``.
926- While other string literals always have a constant value, formatted strings
927- are really expressions evaluated at run time.
932+ that is prefixed with ``'f' `` or ``'F' ``.
933+ Unlike other string literals, f-strings do not have a constant value.
934+ They may contain *replacement fields *, which are expressions delimited by
935+ curly braces ``{} ``, which are evaluated at run time.
936+ For example::
937+
938+ >>> f'One plus one is {1 + 1}.'
939+ 'One plus one is 2.'
940+
928941
929942Escape sequences are decoded like in ordinary string literals (except when
930943a literal is also marked as a raw string). After decoding, the grammar
931944for the contents of the string is:
932945
933- .. productionlist :: python-grammar
934- f_string: (`literal_char ` | "{{" | "}}" | `replacement_field `)*
935- replacement_field: "{" `f_expression ` ["="] ["!" `conversion `] [":" `format_spec `] "}"
936- f_expression: (`conditional_expression ` | "*" `or_expr `)
937- : ("," `conditional_expression ` | "," "*" `or_expr `)* [","]
938- : | `yield_expression `
939- conversion: "s" | "r" | "a"
940- format_spec: (`literal_char ` | `replacement_field `)*
941- literal_char: <any code point except "{", "}" or NULL>
946+ .. grammar-snippet :: python-grammar
947+ :group: python-grammar
948+
949+ FSTRING_START: `fstringprefix ` ("'" | '"' | "'''" | '"""')
950+ FSTRING_MIDDLE:
951+ | <any `source_character`, except backslash, newline, '{' and '}'>
952+ | `stringescapeseq`
953+ | "{{"
954+ | "}}"
955+ | <newline, in triple-quoted f-strings only>
956+ FSTRING_END: ("'" | '"' | "'''" | '"""')
957+ fstringprefix: <("f" | "fr" | "rf"), case-insensitive>
958+ f_debug_specifier: whitespace* '=' whitespace*
959+
960+ .. grammar-snippet :: python-grammar
961+ :group: python-grammar
962+
963+ fstring: `FSTRING_START ` `fstring_middle`* `FSTRING_END `
964+ fstring_middle:
965+ | `fstring_replacement_field`
966+ | `FSTRING_MIDDLE`
967+ fstring_replacement_field:
968+ | '{' `f_expression` [`f_debug_specifier`] [`fstring_conversion`]
969+ [`fstring_full_format_spec`] '}'
970+ fstring_conversion:
971+ | "!" ("s" | "r" | "a")
972+ fstring_full_format_spec:
973+ | ':' `fstring_format_spec`*
974+ fstring_format_spec:
975+ | `FSTRING_MIDDLE`
976+ | `fstring_replacement_field`
977+ f_expression:
978+ | ','.(`conditional_expression` | "*" `or_expr`)+ [","]
979+ | `yield_expression`
980+
981+
982+ ---------------
983+
984+
985+
942986
943987The parts of the string outside curly braces are treated literally,
944988except that any doubled curly braces ``'{{' `` or ``'}}' `` are replaced
0 commit comments