Rob Cliffe wrote:
> Your docs link states "... they allow you to pass on the string quote
> character by escaping it with a backslash."
>
> I don't have access to a Python 3 version right now, but that is not
> true in Python 2:
> [snip]

The behavior is the same on Python 3.8.2:

Python 3.8.2 (default, Feb 26 2020, 22:21:03)
[GCC 9.2.1 20200130] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> r'x\'y'
"x\\'y"
>>> list(_)
['x', '\\', "'", 'y']

What is unclear to me (regarding the quoted line from the Design FAQ) is that backslashes can be used in normal strings
to properly escape the same quote type without the above issue:

>>> 'x\'y'
"x'y"
>>> list(_)
['x', "'", 'y']

Perhaps the "they" is intended to mean something other than "raw strings". Based on the context, I think "they" might be instead referring to the previously mentioned text processors (such as regex engines). As in "In return, [those processors] allow you to pass on the string quote character by escaping it with a backslash". Or perhaps the above behavior wasn't always the case. Either way, I think that part of the Design FAQ could benefit from an update to make that more clear, I can see how the current wording could be a bit ambiguous.

Unless I'm mistaken about the intended meaning, the above fix could make for a decent "newcomer friendly"/easy issue. I can open an issue on bugs.python.org for someone else to work on as a first PR; it would be a very straightforward (but still helpful) trivial fix.

On Sun, Mar 15, 2020 at 6:09 PM Rob Cliffe via Python-ideas <python-ideas@python.org> wrote:

On 22/02/2020 06:26, Steven D'Aprano wrote:
>
> Actually, in Python, regexes are the primary reason raw strings were
> added!
>
> Raw strings aren't quite fully raw, which is why you can't use raw
> strings for Windows paths:
>
>      path = r'somewhere\some\folder\'
>
> doesn't work. The reason is that "raw" (semi-cooked?) strings are
s/are/were/
>   
> intended for regexes, not as a general mechanism for disabling string
> escape codes, and regexes aren't allow to end with a bare backslash.
>
>
> https://docs.python.org/3/faq/design.html#why-can-t-raw-strings-r-strings-end-with-a-backslash

So maybe it's time to make raw strings really raw?  They do have uses
other than regexes, as your path example shows.

I've been bitten by this gotcha a few times.

Your docs link states "... they allow you to pass on the string quote
character by escaping it with a backslash."

I don't have access to a Python 3 version right now, but that is not
true in Python 2:

Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit
(AMD64)] on win32

Type "help", "copyright", "credits" or "licence" for more information.

 >>> r'x\'y'

"x\\'y"

 >>> list(_)

['x', '\\', "'", 'y']

(Apologies for the double vertical spacing, I'm wrestling with my email
server.)

Rob Cliffe
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/IABDEOMCX4UDXATLW5LCPKOLISE2GFPV/
Code of Conduct: http://python.org/psf/codeofconduct/