Backslash problem in Python

Source: Internet
Author: User
Tags expression engine

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.