Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape “\” characters in python

Tags:

python

regex

i am very new to regular expression and trying get "\" character using python

normally i can escape "\" like this

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

output

\
i am \nit

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

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

and return me output

['\\']

can someone please explain why

like image 493
nitesh sharma Avatar asked Apr 27 '12 11:04

nitesh sharma


People also ask

How do I escape a character in Python?

To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.

How do you escape characters?

Use the backslash character to escape a single character or symbol. Only the character immediately following the backslash is escaped.

How do you avoid special characters in Python?

Remove Special Characters From the String in Python Using the str. isalnum() Method. The str. isalnum() method returns True if the characters are alphanumeric characters, meaning no special characters in the string.


2 Answers

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: "\\".

like image 60
huon Avatar answered Oct 05 '22 15:10

huon


can someone please explain why

Because re.findall found one match, and the match text consisted of a backslash. It gave you a list with one element, which is a string, which has one character, which is a backslash.

That is written ['\\'] because '\\' is how you write "a string with one backslash" - just like you had to do when you wrote the example code print "\\".

like image 38
Karl Knechtel Avatar answered Oct 05 '22 16:10

Karl Knechtel