Skip to content
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

Add clarification of the r string modifier #9571

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions doc/data/messages/a/anomalous-backslash-in-string/details.rst
@@ -1,2 +1,25 @@
``\z`` is same as ``\\z`` because there's no escape sequence for ``z``. But it is not clear
for the reader of the code.

Background

Python's string literals use the backslash for their own escape
sequences (like \n for a newline). When Python sees an escape sequence
it doesn't recognize, such as "\." (a literal "dot" character in regex),
it gives a DeprecationWarning.

To avoid this warning, you can use a raw string literal for your regular
expression. Raw string literals don't treat the backslash as a special
character and are often used for regular expressions in Python.

By adding the r before the string, you're telling Python to treat this
as a raw string literal, so it won't try to interpret \. as an escape
sequence and will instead ignore it.

An alternative would be to use double backslash \\. The first backslash
escapes the second one when the string is parsed by python. The second
backslash works as the escape sequence for the . when sent to the regex
engine for parsing. In other words, '\\.' becomes '\.' when parsed by
python, and it can then be used in a regex. The "r" before the string
tells python to treat the string as a "raw" string literal (and not to
modify it).