May 2019
Beginner
528 pages
29h 51m
English
Recall that backslash characters in strings introduce escape sequences—like \n for newline and \t for tab. So, if you wish to include a backslash in a string, you must use two backslash characters \\. This makes some strings difficult to read. For example, Microsoft Windows uses backslashes to separate folder names when specifying a file’s location. To represent a file’s location on Windows, you might write:
In [1]: file_path = 'C:\\MyFolder\\MySubFolder\\MyFile.txt'In [2]: file_pathOut[2]: 'C:\\MyFolder\\MySubFolder\\MyFile.txt'
For such cases, raw strings—preceded by the character r—are more convenient. They treat each backslash as a regular character, rather than the beginning of an escape sequence:
In [3]: file_path ...Read now
Unlock full access