Table of Contents » Reference Materials : Escape Sequences
Escape Sequences
In Python programming, escape sequences are special characters used within strings to represent characters that can't be easily typed or have special meaning in Python. Prefaced by a backslash (\), these sequences are interpreted by Python to perform specific functions or represent specific characters. Common escape sequences include \n for a newline, \t for a tab, \\ for a literal backslash, \' for a single quote, and \" for a double quote. These sequences are essential for including characters in a string that would otherwise be interpreted differently by Python. For instance, the newline escape sequence (\n) is widely used to break text into multiple lines. Escape sequences enable more precise control over the formatting and representation of strings in Python code, allowing developers to include a range of characters that are not easily typable or would disrupt the string's syntax if used directly.
Escape Sequence |
Description | Code | Output |
---|---|---|---|
\\ | Prints a backslash character. |
|
\ |
\' | Prints a single quote mark. |
|
' |
\" | Prints a double quote mark. |
|
" |
\b | Backspace - removes the previous character in the string. |
|
Tes String |
\f | Formfeed (FF) |
|
Test String |
\n | Linefeed (LF) |
|
Test String |
\N{name} | Prints a character from the Unicode Character Database. You can find an index of the available emoji characters at here. |
|
😀 |
\t | Horizontal tab (TAB). Inserts a tab at the point of the \t sequence. |
|
ABCD EFGH |