Why can a quote be escaped in a raw string?

Most discussions of raw strings (including the introduction in Section 2.4.1 of the official documentation) state that backslashes in raw strings are treated literally, rather than being interpreted as the first character of an escape sequence with a special meaning (\t, \n, etc.).

Yet, as stated in the last para of Section 2.4.1, “Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result.”

Why was an exception made for the quote characters? And then, why does the backslash remain in the result? Why can’t a string containing just the backslash character be represented as r’’, r"" or r""""""? If I need to include some kind of quote in a raw string, I could always use a different kind of quote as the string delimiter (e.g. " ’ ").

To me, all this goes absolutely against the “principle of least surprise”, so I’m guessing I must be missing something, and that there must have been extensive(?) discussion about this within the Python developer community. I’d be grateful if someone could please point me to that discussion. Or to a forum that’s more appropriate for this question.


Why can’t a string containing just the backslash character be represented as
r’’, r"" or r""""""?

Answering just the question above from your last message, quoting the docs:

r"" is not a valid string literal (even a raw string cannot end in an odd
number of backslashes). Specifically, a raw literal cannot end in a single
backslash (since the backslash would escape the following quote character).

You probably have seen this in your research already.

Yes, thanks! My question is really about the developers’ rationale behind this design decision.


Perfect, thank you very much for this pointer (and the parent node)!

This post is an off-topic comment on the linked FAQ just to qualify the following statement, which isn’t universally true:

If you’re trying to build Windows pathnames, note that all Windows system
calls accept forward slashes too

By one common definition of a “system call” (i.e. a direct call into the kernel), pretty much no (probably zero) system calls support slash instead of backslash as the path separator. But in Windows the term “system call” is often used for any call into a system library function – e.g. CreateFileW, in addition to the direct system call NtCreateFile.

Windows API filesystem functions such as CreateFileW support slash as a path separator by creating a normalized copy of the path that replaces slashes with backslashes. System library functions for other path namespaces in Windows usually do not do this and thus require backslash as the path separator, e.g. registry paths such as winreg.OpenKey(hkey, r"relative\subkey\path").

Even some file API calls only support backslash, such as the path API. For example:

>>> path = ctypes.WinDLL('api-ms-win-core-path-l1-1-0')
>>> path.PathCchIsRoot('\\')
>>> path.PathCchIsRoot('/')
>>> p = (ctypes.c_wchar * 100)()
>>> path.PathCchCanonicalizeEx(p, 100, r'spam\..\eggs', 0)
>>> p.value
>>> path.PathCchCanonicalizeEx(p, 100, 'spam/../eggs', 0)
>>> p.value

Python scripts use os.path and pathlib, so fortunately there isn’t much need for Windows API path functions that only support backslash.