3.3.2. Backslash#
Inserting a backslash \
in a string tells Python that the following character is a special character. It may be used to enter quotes, line breaks or backslashes in a string.
3.3.2.1. Single-quote \'
and double-quote \"
#
If a same string needs to include both apostrophes and double-quotes, then it is impossible to select a correct delimiter. Backslashing a quote character (\'
, \"
) tells python that this really is a character, and not a delimiter. For instance:
example1 = "I didn't write \"E = mc2\", Einstein did."
example2 = 'I didn\'t write "E = mc2", Einstein did.'
print(example1)
print(example2)
I didn't write "E = mc2", Einstein did.
I didn't write "E = mc2", Einstein did.
3.3.2.2. Line break: \n
#
Newline characters are inserted using \n
:
example_string = "I ate your sandwich.\nIt was good."
print(example_string)
I ate your sandwich.
It was good.
3.3.2.3. Backslash: \\
#
To include a backslash in the string, we need to backslash it so that Python sees it as a character instead of a backslash command:
file_path = "C:\\Windows\\important_driver.dll"
print(file_path)
C:\Windows\important_driver.dll