Python string literals are kinda funny

(sebsite.pw)

5 points | by jandeboevrie 10 hours ago ago

2 comments

  • xg15 8 hours ago

    here's a valid f-string:

    >>> f'{'}'}' '}'

    Huh? I remember learning the rule that you can't nest quotes inside fstrings if they are the same kind (unlike in bash) - e.g

      f"{mydict["foo"]}"
    
    would be a syntax error, but

      f"{mydict['foo']}"
    
    or

      f'{mydict["foo"]}'
    
    would be valid.

    The reason being the same like for the rstring weirdness: The lexer comes first and identifies the string literal, then for fstrings, the python parser is invoked again for each {...} expression to parse it.

    This is unlike other nested expressions, which are already split up by the lexer and then parsed in one go.

    Did that change at some point?