Escape sequences are special characters used inside string literals to represent certain whitespace, formatting, or control characters that are otherwise difficult to include directly in a string. They begin with a backslash (\\
) followed by a character or sequence that specifies the desired special behavior.
Escape sequences allow us to:
In this chapter, we will explore the following escape sequences in Python:
\n
) - Moves the cursor to the next line\r
) - Returns the cursor to the start of the line\b
) - Deletes the character before the cursor\\
) - Inserts a literal backslash character\"
) - Inserts a double quote inside a string\'
) - Inserts a single quote inside a string\t
) - Inserts a horizontal tab space\a
) - Produces an alert sound (if supported)Each of these will be covered in detail with examples in the following pages.