Why can't Python's raw string literals end with a single backslash? -
technically, odd number of backslashes, described in the docs.
>>> r'\' file "<stdin>", line 1 r'\' ^ syntaxerror: eol while scanning string literal >>> r'\\' '\\\\' >>> r'\\\' file "<stdin>", line 1 r'\\\' ^ syntaxerror: eol while scanning string literal it seems parser treat backslashes in raw strings regular characters (isn't raw strings about?), i'm missing obvious. tia!
the reason explained in part of section highlighted in bold:
string quotes can escaped backslash, backslash remains in string; example,
r"\""valid string literal consisting of 2 characters: backslash , double quote;r"\"not valid string literal (even raw string cannot end in odd number of backslashes). specifically, raw string cannot end in single backslash (since backslash escape following quote character). note single backslash followed newline interpreted 2 characters part of string, not line continuation.
so raw strings not 100% raw, there still rudimentary backslash-processing.
Comments
Post a Comment