@@ -1026,6 +1026,46 @@ What does 'UnicodeDecodeError' or 'UnicodeEncodeError' error mean?
1026
1026
See the :ref: `unicode-howto `.
1027
1027
1028
1028
1029
+ .. _faq-programming-raw-string-backslash :
1030
+
1031
+ Can I end a raw string with an odd number of backslashes?
1032
+ ---------------------------------------------------------
1033
+
1034
+ A raw string ending with an odd number of backslashes will escape the string's quote::
1035
+
1036
+ >>> r'C:\this\will\not\work\'
1037
+ File "<stdin>", line 1
1038
+ r'C:\this\will\not\work\'
1039
+ ^
1040
+ SyntaxError: unterminated string literal (detected at line 1)
1041
+
1042
+ There are several workarounds for this. One is to use regular strings and double
1043
+ the backslashes::
1044
+
1045
+ >>> 'C:\\this\\will\\work\\'
1046
+ 'C:\\this\\will\\work\\'
1047
+
1048
+ Another is to concatenate a regular string containing an escaped backslash to the
1049
+ raw string::
1050
+
1051
+ >>> r'C:\this\will\work' '\\'
1052
+ 'C:\\this\\will\\work\\'
1053
+
1054
+ It is also possible to use :func: `os.path.join ` to append a backslash on Windows::
1055
+
1056
+ >>> os.path.join(r'C:\this\will\work', '')
1057
+ 'C:\\this\\will\\work\\'
1058
+
1059
+ Note that while a backslash will "escape" a quote for the purposes of
1060
+ determining where the raw string ends, no escaping occurs when interpreting the
1061
+ value of the raw string. That is, the backslash remains present in the value of
1062
+ the raw string::
1063
+
1064
+ >>> r'backslash\'preserved'
1065
+ "backslash\\'preserved"
1066
+
1067
+ Also see the specification in the :ref: `language reference <strings >`.
1068
+
1029
1069
Performance
1030
1070
===========
1031
1071
0 commit comments