Because \d
is not an escape code. So, however you type it, it is interpreted as a literal \
then a d
.
If you type \\d
, then the \\
is interpreted as an escaped \
, followed by a d.
The situation is different if you choose a letter part of an escape code.
r'\n+','\n+', '\\n+'
⇒ ('\\n+', '\n+', '\\n+')
The first one (because raw) and the last one (because \ is escaped) is a 3-letter string containing a \
a n
and a +
.
The second one is a 2 letter string, containing a '\n'
(a newline) and a +
The second one is even more straightforward. Nothing strange here. r'\1'
is a backslash then a one. '\1'
is the character whose ASCII code is 1, whose canonical representation is '\x01'
'\1'
, '\x01'
or '\001'
are the same thing. Python cannot remember what specific syntax you used to type it. All it knows is it that is the character of code 1. So, it displays it in the "canonical way"
.
Exactly like 'A'
'\x41'
or '\101'
are the same thing. And would all be printed with the canonical representation, which is 'A'