Home » Programming » Python » Python Escape Characters

Escape Characters in Python, With Examples

Every programming language has special characters that trigger certain behaviors in your code – and Python is no exception.

These characters cannot be used in strings (as they are reserved for use in your code) – and must be “escaped” to tell the computer that the character should just be treated as a regular piece of text rather than something that needs to be considered while the code is being executed.

If you want to be able to leave yourself notes in your code, check out our rundown on multi-line commenting in Python.

Escaping Special Characters with ‘\’ (Backslash)

The escape character in Python (and a few other programming languages) is the standard backslash. Placing a ‘\’ in front of one of these special or reserved characters will tell Python to just treat it as text, ensuring your code is valid and behaves as you’d expect.

Example

The easiest way to demonstrate what can happen when you use special characters in a string without escaping them is to use double quotes:

test = "To quote a great philosopher - "It's Tricky""
print test

If you try to run this you’ll get something like:

SyntaxError: invalid syntax

…because the quotes in the text we want to display are pre-maturely terminating the string and creating invalid code.

To get around this, we add some backslashes to the double quotes we only want to display as part of the text:

test = "To quote a great philosopher - \"It's Tricky\""
print test

Single quotes are not considered special characters in this sentence as the string is defined using double-quotes. If the string was defined by containing it within single quotes, the single quote in it’s would need to be escaped instead of the double-quotes.

List Of Character Escape Sequences

Below is a list of Characters in Python with escape character sequences. Many of these you will likely never encounter – so the common ones are marked.

Escaped Character Outputted Text Common Usage
\\ \ \
\’
\”
\newline Ignored
\a ASCII BELL BEL
\b ASCII BACKSPACE BS
\f ASCII FORMFEED FF
\n ASCII LINEFEED LF New Line
\r ASCII CARRAIGE RETURN CR New Line (Older Operating Systems)
\t ASCII HORIZONTAL TAB TAB tab
\v ASCII VERTICAL TAB VT
\ooo ASCII OCTAL
\xhh ASCII HEX
\N{NAME} UNICODE CHARACTER {NAME}
\0 NULL
\uhhhh 16 BIT UNICODE CHARACTER
\uhhhhhhhh 32 BIT UNICODE CHARACTER

Conclusion

If you have code that won’t execute due to syntax errors and can’t find the cause, comb through your strings and user input and make sure it’s all properly escaped! It’s a common cause of code issues and issues with special characters can easily fly under the radar when debugging.

Click here for more Python explainers and tutorials!

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment