EDIT: The problem is actually how print
works with lists & strings. It prints the representation of the string, not the string itself, the representation of a string containing just a backslash is '\\'
. So findall
is actually finding the single backslash correctly, but print
isn't printing it as you'd expect. Try:
>>> print(re.findall(r'\\',"i am \\nit")[0])
\
(The following is my original answer, it can be ignored (it's entirely irrelevant), I'd misinterpreted the question initially. But it seems to have been upvoted a bit, so I'll leave it here.)
The r
prefix on a string means the string is in "raw" mode, that is, \
are not treated as special characters (it doesn't have anything to do with "regex").
However, r'\'
doesn't work, as you can't end a raw string with a backslash, it's stated in the docs:
Even in a raw string, string quotes can be escaped with a backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character).
But you actually can use a non-raw string to get a single backslash: "\\"
.