Python regex '\s' vs '\\s'

python, regex

Solution

Don't confuse python-level string-escaping and regex-level string-escaping. Since `s` is not an escapable character at python-level, the interpreter understand a string like "\s" as the two characters "\" and "s". Replace "s" with "n" (for example), and it understands it as the newline character.

'\s' == '\\s'
True
'\n' == '\\n'
False

Problem

I have simple expression `\s` and `\\s`. Both expression matches `This is Sparta!!`. ``` >>> re.findall('\\s',"This is Sparta") [' ', ' '] >>> re.findall('\s',"This is Sparta") [' ', ' '] ``` I am confused here. `\` is used to escape special character and `\s` represents white space but, how both are acting here?

Original source