
On Mon, Mar 16, 2020 at 9:08 AM 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-en...
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."
Currently, string prefixes don't determine when the string ends. Neither raw strings nor f-strings can end the string anywhere other than the place a vanilla string literal would:
f"asdf{'qwer"zxcv'}1234" File "<stdin>", line 1 f"asdf{'qwer"zxcv'}1234" ^ SyntaxError: invalid syntax f"""asdf{'qwer"zxcv'}1234""" 'asdfqwer"zxcv1234'
I don't know how deeply baked into the language this requirement is, but it's certainly something that makes things easier for all forms of syntax highlighting etc. ChrisA