Slash in Python

Source: Internet
Author: User
Tags printable characters
This article mainly introduces the information about the left and right slashes (forward and backward slashes) in Python. it is very good and has reference value. if you need it, refer to the following, "/" The left skew is a forward slash, "\" The right skew is a backslash, which can be recorded as: the forward slash is a forward slash. In general, a forward slash/is used for directory separators, 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 "\\".

The above section describes the left and right slashes (forward and backward slashes) in Python. I hope they will help you. if you have any questions, please leave a message, the editor will reply to you in a timely manner. I would like to thank you for your support for PHP chinnet!


For more articles about slashes in Python, refer to the PHP Chinese website!

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.