diff --git a/Doc/tutorial/introduction.rst b/Doc/tutorial/introduction.rst index 8763626ef553b98..edf4b1df6b7a10a 100644 --- a/Doc/tutorial/introduction.rst +++ b/Doc/tutorial/introduction.rst @@ -182,6 +182,29 @@ the first quote:: >>> print(r'C:\some\name') # note the r before the quote C:\some\name +There is one subtle aspect to raw strings that is of special concern to Windows +programmers: a raw string may not end in an odd number of ``\`` characters. +This is because the interpreter sees this as an escaped quote character, and so +it does not recognize it as the end of the string:: + + >>> fn = r'C:\this\will\not\work\' + File "", line 1 + fn = r'C:\this\will\not\work\' + ^ + SyntaxError: EOL while scanning string literal + +There are several workarounds for this. One is to use regular strings and +double the backslashes:: + + >>> fn = 'C:\\this\\will\\work\\' + +Another is to add a blank character before the quote and then remove it:: + + >>> fn = r'C:\this\will\work\ '.strip() + +Strings can be concatenated (glued together) with the ``+`` operator, and + repeated with ``*``:: + String literals can span multiple lines. One way is using triple-quotes: ``"""..."""`` or ``'''...'''``. End of lines are automatically included in the string, but it's possible to prevent this by adding a ``\`` at diff --git a/Misc/NEWS.d/next/Documentation/2021-08-26-16-53-22.bpo-11479.zHAOnZ.rst b/Misc/NEWS.d/next/Documentation/2021-08-26-16-53-22.bpo-11479.zHAOnZ.rst new file mode 100644 index 000000000000000..1e10c89d2522d79 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2021-08-26-16-53-22.bpo-11479.zHAOnZ.rst @@ -0,0 +1 @@ +Added a discussion of trailing backslash in raw string to tutorial \ No newline at end of file