If you run those strings against re
instead you’ll see something different. E.g. via re.match
:
0>>> import re
1>>> re.match(".", "K") # standard string, no escape, matches any character
2<re.Match object; span=(0, 1), match='K'>
3>>> re.match("\.", "K") # standard string, with escape, does not match any character (hence no result)
4>>> re.match("\.", ".") # standard string, with escape, does match "."
5<re.Match object; span=(0, 1), match='.'>
6>>> re.match(r"\.", "K") # raw string, with escape, does not match any character
7>>> re.match(r"\.", ".") # raw string, with escape, does match "."
8<re.Match object; span=(0, 1), match='.'>
9>>> re.match("\\.", "K") # standard string, double escape, does not match any character
10>>> re.match("\\.", ".") # standard string, double escape, does match "."
11<re.Match object; span=(0, 1), match='.'>
12>>> re.match(r"\\.", "K") # raw string, double escape, does not match any character
13>>> re.match(r"\\.", ".") # raw string, double escape, does not match "."
You can see above that absent the \
, “.” is interpreted as any character, while “\.”, r"\." and “\\.” are both interpreted as an escaped period. Python is planning to error on “\.” in the future.
https://bugs.python.org/issue27364