8000 gh-55688: Add note about ending backslashes for raw strings by slateny · Pull Request #94768 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-55688: Add note about ending backslashes for raw strings #94768

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

Merged
merged 6 commits into from
Dec 28, 2022
Merged
Changes from 2 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
24 changes: 24 additions & 0 deletions Doc/tutorial/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,30 @@ 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: a raw string may not end in an odd
Copy link
Contributor

Choose a reason for hiding this comment

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

The text of the addition is fine. But I am wondering whether such a subtle point should be part of the introduction.rst. Perhaps keep only the first line and refer to a location with details?

(not sure what would be a better location)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not strictly a part of this file, see https://docs.python.org/3/reference/lexical_analysis.html:

... even a raw string cannot end in an odd number of backslashes ...

Copy link
Contributor

Choose a reason for hiding this comment

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

The text you added in this PR will end up here: https://docs.python.org/3/tutorial/introduction.html#strings right?

It is an informal introduction, so I would write something like:

There is one subtle aspect to raw strings: a raw string may not end in an odd
number of \ characters. For details see String and Bytes literals

But the text added is correct, so it is mostly a matter of style.

Copy link
Contributor Author
@slateny slateny Dec 24, 2022

Choose a reason for hiding this comment

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

Re-reading this, I realize that my comment above doesn't actually address your original concern on whether this is too subtle of a point for the tutorial introduction - IMO, since the section above does talk about escaping quotes as well as raw strings, I think it's somewhat natural to at least mention the case with them combined.

As for how brief a mention, the current phrasing can definitely be made more brief. I think also that just one of the workarounds can be left in, while the other two examples put elsewhere, along with the wording specific to Windows paths. If the section would introduce the problem of raw strings not ending in an odd number of slashes, I think it should also suggest some sort of solution to it, so I would prefer keeping one example in.

As for moving the rest of the information, I don't think the reference is the most appropriate place, maybe instead in some FAQ entry or a howto page?

Copy link
Contributor
@hauntsaninja hauntsaninja Dec 24, 2022

Choose a reason for hiding this comment

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

An FAQ entry sounds reasonable. Maybe we do something like There is one subtle aspect to raw strings: a raw string may not end in an odd number of \ characters; see [the FAQ entry] for workarounds?

number of ``\`` characters. That is of special concern while dealing with Windows
paths. 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: unterminated string literal (detected at line 1)


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()

It is also possible to use :func:`os.path.join` to append a backslash on Windows
(by leaving the last parameter empty).

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
0