Python Escape Characters and Raw Strings

1. Escape Characters:

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
print("Hello\nWorld") # \n creates a newline.
print("This is a backslash: \\") # \\ inserts one backslash.
print('She said, "It\'s a nice day!"') # \' or \" inserts quote characters into strings
print("C:\\Users\\New folder ") # Note no \n so it stays on single line. Try with print("C:\\Users\\\nNew folder ") if same meaning required, like folder structure, by only newline intended before last directory name (or subdirectory if relevant). Could apply escape to prior folder/file/variable character components/elements individually to keep new line escape character at outermost position of string. Use triple quotes or similar as required if whole folder or file names known and don't end up conflicting within particular program scope at parse-time rather than needing special characters embedded inside so avoid clashes

2. Raw Strings:

Escape characters with \ don't trigger in a “raw” string, denoted using r"String Content...", string contents interpreted verbatim

print(r"Hello\nWorld") # Note: Raw strings prevent interpretation like newline via \n sequence if it is desired that such a sequence or other escape codes remain exactly what they are for print or formatting reasons etc. so raw string keeps them in place

When to Use Raw Strings:

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

    filepath= r"C:\User\Document\New Text Document.txt"
  • Regular expressions containing lots of escape sequences can be defined via the r'your regex..'