The reason backslashes don't currently work is because of how they'd interact with string parsing. I don't think this will change until we move f-string parsing into the grammar itself, instead of happening as a post-processing step in ast.c. See bpo-33754 for one example of this. There have been numerous discussions about it over the years.

As for the "\ " change: I hesitate to comment until f-strings are moved into the grammar and we can see how they work.

Eric

On 6/29/2020 10:29 AM, Mikhail V wrote:
Proposal:

Allow standard python escapes inside curly braces in f-strings.
Main point is to make clear visual distinction between text and
escaped chars:

# current syntax:
       print ("\nnewline")
       print ("\x0aa")

# new syntax:
       print (f"{\n}newline")
       print (f"{\x0a}a")

Currently it is SyntaxError:
     "SyntaxError: f-string expression part cannot include a backslash"

Further, I suggest hex code escapes with a new prefix "\ ", i.e.
backslash+space,
(this would work in f-strings only obviously) so it could be used
instead current
variants: \\x, \\u, and \\U without need to include all leading zeros in codes.
Consecutive codes can be simply separated by space.

Example:

# current syntax:
       print ("\x48\x65\x6c\x6c\x6f\U0001F601")      # Hello and a smiley
       print ("\x0aa")

# new syntax:
       print (f"{\ 48 65 6c 6c 6f 01F601}")
       print (f"{\ 0a}a")

And I personally would like to see an option for decimal charcodes,
e.g. with "\." prefix using the same schema as above with hex codes.


Mikhail
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/G6BXGJTRSPVK5VC6FT33VAGWVJOEGOWV/
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5NIOHCOUQWL4JOX75J4AKA6KLUTENN5B/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to