r'\\'
is a two-character string composed of \
and \
. repr()
gets its representation as a Python string literal. As backslashes start escape sequences in string literals, they themselves need to be escaped to accurately reproduce the original string, so repr()
returns '\\\\'
– a string composed of six characters. Finally, when displaying this string, the Python interactive shell itself uses repr()
, which selects double quotes to avoid having to escape the '
in the string it’s trying to represent, and escapes each of the backslashes again, resulting in what you see.
r'\\'
: \\
repr(r'\\')
: '\\\\'
repr(repr(r'\\'))
: "'\\\\\\\\'"