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." 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