The forward slash, backslash, and python slash in python

Source: Internet
Author: User
Tags printable characters

[Go] forward slash, backslash, and python slash in python

Address: http://www.cnblogs.com/followyourheart1990/p/4270566.html

First, "/" is a forward slash and "\" is a backslash. It can be recorded as a forward slash. In general, a forward slash/is used for directory separators, and Unix and Web /, windows uses a backslash, but now Windows

 

(1) slash in the directory

The directory parameters that need to be input for reading python files. The following examples are listed:

Path = r "C: \ Windows \ temp \ readme.txt"

Path1 = r "c: \ windows \ temp \ readme.txt"

Path2 = "c :\\ windows \ temp \ readme.txt"

Path3 = "c:/windows/temp/readme.txt"

The parameters in open () can be path, path1, path2, or path3.

 

Path: "\" is a special character in the string. if r is added to the original string, "\ t" and "\ r" in the string are not escaped.

Path1: Case sensitivity does not affect file location in windows

Path2: cancels the special escape Function of the second "\" with a "\", that is "\\"

Path3: Directory separators can also be converted to the corresponding directory by using a forward slash, and the path3 method in python saves the troubles of backslash \ escape.

 

 

(2) slashes in Regular Expressions

The regular expression matches the Backslash "\". Why is it "\" or r?

Because \ is a special symbol in a regular expression, in order to cancel its special meaning in a regular expression, you need to add \ to become \, but the problem comes again, \ is also a special character in the string, so it must be canceled for both \, that is \\\\. In Python, there is an original string operator used for special characters in the strings. In the original strings, there are no escape characters or printable characters. In this way, the escape Function of \ In the string can be canceled, that is, r "\\".

Related Article

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.