Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 23 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()

@Julian Julian Aug 25, 2021

Copy link
Copy Markdown

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' '\\'

@dancinglightning dancinglightning Aug 25, 2021

Copy link
Copy Markdown
Author

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 ``*``::
Comment thread
dancinglightning marked this conversation as resolved.

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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.