8.11 Raw Strings

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_path
Out[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 = r'C:\MyFolder\MySubFolder\MyFile.txt'

In [4]: file_path
Out[4]: 'C:\\MyFolder\\MySubFolder\\MyFile.txt'

Python converts the raw string to a regular string that still uses the two backslash characters in its internal representation, as shown in the last snippet. Raw strings can make your code more readable, particularly when using the regular expressions that we discuss in the next section. Regular expressions often contain many backslash characters.

tick mark Self Check

  1. (Fill-In) The raw string r'\\Hi!\\' represents the regular string      .
    Answer: '\\\\Hi!\\\\'.