Added some text about escaping quote chars, and about raw f-strings.

This commit is contained in:
Eric V. Smith 2015-09-08 20:10:44 -04:00
parent 5189173b1e
commit ebbbc136c1
1 changed files with 25 additions and 3 deletions

View File

@ -183,9 +183,9 @@ for strings should be trivially modifiable to recognize f-strings
(parsing within an f-string is another matter, of course). (parsing within an f-string is another matter, of course).
Once tokenized, f-strings are decoded. This will convert backslash Once tokenized, f-strings are decoded. This will convert backslash
escapes such as ``'\n'``, ``'\xhh'``, ``'\uxxxx'``, ``'\Uxxxxxxxx'``, escapes such as ``'\n'``, ``'\"'``, ``"\'"``, ``'\xhh'``,
and named unicode characters ``'\N{name}'`` into their associated ``'\uxxxx'``, ``'\Uxxxxxxxx'``, and named unicode characters
Unicode characters [#]_. ``'\N{name}'`` into their associated Unicode characters [#]_.
Up to this point, the processing of f-strings and normal strings is Up to this point, the processing of f-strings and normal strings is
exactly the same. exactly the same.
@ -256,6 +256,28 @@ resulting string value is to double the brace::
>>> f'{{{4*10}}}' >>> f'{{{4*10}}}'
'{40}' '{40}'
Like all raw in Python, no escape processing is done for raw
f-strings::
>>> fr'x={4*10}\n'
'x=40\\n'
Due to Python's string tokenizing rules, the f-string
``f'abc {a['x']} def'`` is invalid. The tokenizer parses this as 3
tokens: ``f'abc {a['``, ``x``, and ``']} def'``. Just like regular
strings, this cannot be fixed by using raw strings. There are a number
of correct ways to write this f-string: with escaped single quotes::
f'abc {a[\'x\']} def'
With a different quote character::
f"abc {a['x']} def"
Or with triple quotes::
f'''abc {a['x']} def'''
Code equivalence Code equivalence
---------------- ----------------