summaryrefslogtreecommitdiffstats
path: root/Doc/faq
diff options
context:
space:
mode:
authorStanley <46876382+slateny@users.noreply.github.com>2022-12-28 05:30:42 (GMT)
committerGitHub <noreply@github.com>2022-12-28 05:30:42 (GMT)
commitb95b1b3b25b0a93a22c7d58ac5bd5870e62070a8 (patch)
tree4bd7d67df5ced8c08b707288a530293fb2e4dfae /Doc/faq
parent5369bba8c594de7a643408550e19e1ff6df5178a (diff)
downloadcpython-b95b1b3b25b0a93a22c7d58ac5bd5870e62070a8.zip
cpython-b95b1b3b25b0a93a22c7d58ac5bd5870e62070a8.tar.gz
cpython-b95b1b3b25b0a93a22c7d58ac5bd5870e62070a8.tar.bz2
gh-55688: Add note about ending backslashes for raw strings (#94768)
Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
Diffstat (limited to 'Doc/faq')
-rw-r--r--Doc/faq/programming.rst40
1 files changed, 40 insertions, 0 deletions
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst
index c396e2b..ba42289 100644
--- a/Doc/faq/programming.rst
+++ b/Doc/faq/programming.rst
@@ -1026,6 +1026,46 @@ What does 'UnicodeDecodeError' or 'UnicodeEncodeError' error mean?
See the :ref:`unicode-howto`.
+.. _faq-programming-raw-string-backslash:
+
+Can I end a raw string with an odd number of backslashes?
+---------------------------------------------------------
+
+A raw string ending with an odd number of backslashes will escape the string's quote::
+
+ >>> r'C:\this\will\not\work\'
+ File "<stdin>", line 1
+ r'C:\this\will\not\work\'
+ ^
+ SyntaxError: unterminated string literal (detected at line 1)
+
+There are several workarounds for this. One is to use regular strings and double
+the backslashes::
+
+ >>> 'C:\\this\\will\\work\\'
+ 'C:\\this\\will\\work\\'
+
+Another is to concatenate a regular string containing an escaped backslash to the
+raw string::
+
+ >>> r'C:\this\will\work' '\\'
+ 'C:\\this\\will\\work\\'
+
+It is also possible to use :func:`os.path.join` to append a backslash on Windows::
+
+ >>> os.path.join(r'C:\this\will\work', '')
+ 'C:\\this\\will\\work\\'
+
+Note that while a backslash will "escape" a quote for the purposes of
+determining where the raw string ends, no escaping occurs when interpreting the
+value of the raw string. That is, the backslash remains present in the value of
+the raw string::
+
+ >>> r'backslash\'preserved'
+ "backslash\\'preserved"
+
+Also see the specification in the :ref:`language reference <strings>`.
+
Performance
===========