Python itself uses \ to escape some special characters, such as when the quotation marks are added to the string
' i\ ' m Superman ' Print (s) # i ' m Superman
In order to prevent and the string itself to escape the quotation marks, use \ To escaping, generally this will not cause any problems, but when you want to use \ to escape the time, it is more chaotic, such as we want to output a \, write two \, otherwise it will report syntax error, because \ put the back of the quotation marks to escape, must use \
# wrong wording # print ' \ ' # correct wording Print ('\ \') # \ # Native String Print (R'\ \') # \\
Will \ escape a bit so that it does not have the escape function, can correctly output, when the use of the original string, the output shows two \, it seems to write a few output a few appearance, if you think so, you can try to see if it can output odd number of \.
Supplementary ——————————————————
python's native string cannot end with a backslash
Python's native string (raw string) is a useful thing to write a few backslashes (escape symbols).
But it has a well-known bug, which is that it can't end with a backslash, which makes a person very silent. Today, I suddenly found out that the general Python FAQ has instructions and solutions.
The first thing to clarify is not to end with a backslash, but not to end with an odd number of backslashes.
Second, the reason for this bug is that some processors, such as the primary regular expression engine, want to do their own backslash escape, which does not allow the string to end with an unmatched backslash.
Finally, the solution is to put the backslash in another string, and Python will merge automatically:
Dir = r"\this\is\my\dos\dir""\ \"
Supplemental End ——————————————————
Although the native string is not perfect, it can help us solve a large part of the problem. For example, when you want to match "\", the original string can let you write at least half of the \, that is, save the code amount, but also increased readability.
Importre_string='\\\\'Print(_string)# \\#string forIinchRe.findall ('\\\\', _string):Print(i)# \ # \#Native String forIinchRe.findall (R'\\', _string):Print(i)# \ # \
Backslash problem in Python