To quote the Python FAQ, raw string literals in Python were “designed to ease creating input for processors (chiefly regular expression engines) that want to do their own backslash escape processing”. Since the regex engine will strip the backslash in front of the quote character, Python doesn’t need to strip it. This behavior will most likely never be changed since it would severely break backwards compatibility.
So yes, it is by design — although it is quite confusing.
I want to know why I can’t use the raw syntax r”” or r” form on this
raw string “word’s” and have it exist in a variable just like this.
Python’s raw string literals were not designed to be able to represent every possible string. In particular, the string "'
cannot be represented within r""
or r''
. When you use raw string literals for regex patterns, this is not a problem, since the patterns \"'
, "\'
, "'
, and \"\'
, are equivalent (that is, they all match the single string "'
).
However, note that you can write the string "word's"
using the triple-quoted raw string literal r'''"word's"'''
.
6
solved Create and parse a Python Raw string literal R”” [duplicate]