Why do 3 backslashes equal 4 in a Python string?
Could you tell me why
True? That drives me crazy and I can't find a reasonable answer...
>>> list('?\\\?') ['?', '\\', '\\', '?'] >>> list('?\\\\?') ['?', '\\', '\\', '?']
Basically, because python is slightly lenient in backslash processing. Quoting from https://docs.python.org/2.0/ref/strings.html :
Unlike Standard C, all unrecognized escape sequences are left in the string unchanged, i.e., the backslash is left in the string.
(Emphasis in the original)
Therefore, in python, it isn't that three backslashes are equal to four, it's that when you follow backslash with a character like
?, the two together come through as two characters, because
\? is not a recognized escape sequence.