How to avoid "\" characters in python

I am very new to regex and trying to get the "\" character using python

I can usually escape "\" like this

print ("\\");
print ("i am \\nit");

conclusion

\
i am \nit

but when i use the same in regX it doesn't work as i thought

print (re.findall(r'\\',"i am \\nit"));

and give me the conclusion

['\\']

can someone explain why

+2
source share
4 answers

EDIT. , print . , , , , - '\\'. , findall , print , . :

>>> print(re.findall(r'\\',"i am \\nit")[0])
\

( , ( ), , , , , .)

r , "" , \ ( " " ).

r'\' , ,

, ; , r "\" " , : ; r "\" ( ). , ( ).

: "\\".

+13

- ,

re.findall , . , , , .

['\\'], '\\' - , " " - , , print "\\".

+1

, - "a string" r"a raw string". , , , . , , , ( , escape-), , , , , , .

0

There is no need to avoid backslashes in raw strings unless the backslash precedes the final quote.

-1
source

All Articles