Skip to content

bpo-11479: Added a discussion of trailing backslash in raw string to tutorial #27949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Doc/tutorial/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<stdin>", 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()
Copy link

@Julian Julian Aug 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I wouldn't mention this one -- it adds a runtime cost if it's inside some loop or function call, which on its own isn't a huge deal, but it also means you get unexpected behavior if you don't fully realize what this does and try it on r' foo bar \ baz quux\ .

I'd personally leave things with the last workaround (and not mention there are multiple), but if another workaround is desired, using two string literals with the terminal slash in a second one implicitly concatenated is the next-best-one to me:

fn = r'C:\this\will\work' '\\'

Copy link
Author

@dancinglightning dancinglightning Aug 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I do agree it adds to the runtime but I am pretty sure that this piece of information can be invaluable and many times go unnoticed. I too faced difficulty in comprehending the issue when I got stuck with it. I was able to resolve it later by reading various discussions.


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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a discussion of trailing backslash in raw string to tutorial