Forward slash and backslash in Python

Source: Internet
Author: User

First, the "/" left tilt is a forward slash, "\" Right tilt is a backslash, can be remembered as: Division sign is a forward slash generally for directory separators, UNIX and the web with a forward slash/,windows with backslashes, but now windows

(i) the slashes in the catalogue

Python reads the directory parameters that the file needs to enter, listing the following examples:

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"

Open file functions The arguments in the open () can be either path or path1, path2, Path3.

Path: "\" is a special character in a string, and R becomes the original string without string escaping for "\ t", "\ R" in the string

Path1: Case does not affect Windows targeting to files

Path2: Use a "\" to cancel the second "\" of the special escape function, that is "\ \"

Path3: Using a forward slash to do a directory separator can also go to the corresponding directory, and the way to path3 in Python also eliminates the anti-slash \ Escape annoyance

(ii) the slashes in regular expressions

The regular expression matches the backslash "\", Why is "\\\\" or r "\ \"?

Because in the regular expression \ is a special symbol, in order to cancel its special meaning in the regular expression needs to add a \ to become the \ \, but the problem comes again, \ is a special character in the string, so we have to separate the two \ Cancel its special meaning, that is \\\\. There is a primitive string operator in Python that is used for special characters in those strings, with no escaped characters in the original string and no printing. This allows you to cancel the escape function in the string, which is r "\ \".

Forward slash and backslash in Python

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.