From e9925ba68af52456e17427166b24983f972747d9 Mon Sep 17 00:00:00 2001 From: Ted Stresen-Reuter Date: Fri, 26 Apr 2024 10:30:28 +0100 Subject: [PATCH] Add clarification of the r string modifier Raw sting literals may not be easily understood based on the brief description. This description is much more verbose (maybe too verbose) but should help the novice python programmer understand what the r modifier is and how / when to use it. --- .../anomalous-backslash-in-string/details.rst | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/doc/data/messages/a/anomalous-backslash-in-string/details.rst b/doc/data/messages/a/anomalous-backslash-in-string/details.rst index e716bc2d9c..a78e23540f 100644 --- a/doc/data/messages/a/anomalous-backslash-in-string/details.rst +++ b/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).