Escape characters are special characters that allow representing and outputting whitespace characters within string, characters that are not directly printable or typable like a newline character without exiting input string or tab stops using character code/notation where normally either invalid characters introduce odd string behaviours within certain Python language constructs if printed, treated like string ends early because a special non-alphanumeric sequence appears causing printing or rendering/truncation behaviours not normally encountered or similar such behaviour unless using string typecasting for such conversions at time before the characters rendered
Use with a backslash `\` within standard/quoted strings to produce whitespace, newlines, special symbols without issues and to distinguish between valid and non-valid parts like in prior example case showing how some variables end or other delimiters or special language keywords can appear inside strings when enclosed in the code notation so as not to terminate earlier due to such nested content
Escape Sequence | Description |
---|---|
\newline |
Backslash and newline ignored |
\\ |
Backslash (\) |
\' |
Single quote (') |
\" |
Double quote (") |
\a |
ASCII Bell (BEL) |
\b |
ASCII Backspace (BS) |
\f |
ASCII Formfeed (FF) |
\n |
ASCII Linefeed (LF) - newline character on Linux |
\r |
ASCII Carriage Return (CR) newline character on older Macs |
\t |
ASCII Horizontal Tab (TAB) |
\v |
ASCII Vertical Tab (VT) |
\ooo |
Character with octal value ooo |
\xhh |
Character with hex value hh |
Escape characters with \ don't trigger in a “raw” string, denoted using r"String Content..."
, string contents interpreted verbatim
Consider in such circumstances if one these conditions seems relevant:
File path representations. In Windows, use of backslashes introduces potential clash since escape sequences involving \ interpreted inside non-raw string type context. Using a raw string helps overcome issue
r'your regex..'