- Python Escape Characters
- Escape Characters
- Single Quote Escape Character
- Double Quote Escape Character
- Backslash Escape Character
- Newline Escape Character
- Carriage Return Escape Character
- Tab Escape Character
- Backspace Escape Character
- Form feed Escape Character
- Octal Value Escape Character
- Hex Value Escape Character
- Summary
- Escape Characters¶
- Notes¶
- Remarks¶
- Example 1¶
- How to Escape Characters in a Python String?
- Method 1: Using re.escape()
- Method 2 : Using String Translate Table
- Method 3: Using Backlash
- Examples for Understanding
- Using re.escape()
- Using String Translate Table
- Learn More
- Conclusion
Python Escape Characters
There are some characters that have a special meaning when used in a string. But what do yo do if you would like to insert that character in the string as is, without invoking its special meaning.
For understanding this, let us take a simple example. We use single quotes or double quotes to define a string. Suppose, we define a string with single quotes. The first occurrence of single quote marks the start of string and the second occurrence marks the end of the string. Now, consider that we would like to have a single quote in our string. What do we do now. If we place a single quote just like that in the middle of string, Python would think that this is the end of the string, which is actually not.
To insert these kind of illegal characters, we need the help of a special character like backslash \.
Escape Characters
Following table provides the list of escape characters in Python.
Code | Description |
---|---|
\’ | Single Quote |
\” | Double Quote |
\\ | Backslash |
\n | New Line |
\r | Carriage Retrun |
\t | Tab |
\b | Backspace |
\f | Form Feed |
\ooo | Octal Value |
\xhh | Hex Value |
Single Quote Escape Character
To escape single quote character, use a preceding backslash for the single quote in the string.
Python Program
If you are using double quotes to define a string, you may not use the escape sequence to escape the single quote. But, even if you use, that does not change the output, anyways.
Python Program
Double Quote Escape Character
To escape double quote character, use a preceding backslash for the double quote in the string.
Python Program
If you are using single quotes to define a string, you may not use the escape sequence to escape the double quote. But, even if you use, that does not change the output.
Python Program
Backslash Escape Character
To escape backslash character, use a preceding backslash for backslash in the string. That would look like two backslashes in the string.
Python Program
Newline Escape Character
To escape newline character, use a preceding backslash for character ‘n’ in the string.
Python Program
Carriage Return Escape Character
To escape carriage return character, use a preceding backslash for character ‘r’ in the string.
Python Program
After printing hello, carriage return will take the cursor to the start of the same line, and then it prints world, which kind of overwrites on the previous data. So, you see only world, but no hello, in the output.
Tab Escape Character
To escape tab character, use a preceding backslash for character ‘t’ in the string.
Python Program
Backspace Escape Character
To escape backspace character, use a preceding backslash for character ‘b’ in the string.
Python Program
After printing hello, a backspace would delete the last character o, and then world is printed out. So, the final result would look like hellworld.
Form feed Escape Character
To escape form feed character, use a preceding backslash for character ‘f’ in the string.
Python Program
Octal Value Escape Character
To escape a byte of octal value character, use a preceding backslash for three digit octal value in the string.
Python Program
Octal value 101 represents A, 102 represents B, and so on. So, in the output, we got ABC for the given octal values.
Hex Value Escape Character
To specify a byte using hex value, use a preceding backslash and x for two digit hex value in the string.
Python Program
Hex value of 41 means 65 in decimal. And a byte with decimal 65 represents the character A. Similarly 42 is B, 43 is C.
Summary
In this tutorial of Python Examples, we learned what escape characters are, how to use them in a string, and their usage, with the help of example programs for each of the escape characters.
Escape Characters¶
\newline Ignored \ Backslash (\) ‘ Single quote (‘) » Double quote (“) \a ASCII Bell (BEL) \b ASCII Backspace (BS) \f ASCII Formfeed (FF) \n ASCII Linefeed (LF) \N Character named NAME in the Unicode database (Unicode only) \r ASCII Carriage Return (CR) \t ASCII Horizontal Tab (TAB) \uxxxx Character with 16-bit hex value XXXX (Unicode only) (1) \Uxxxxxxxx Character with 32-bit hex value XXXXXXXX (Unicode only) (2) \v ASCII Vertical Tab (VT) \ooo Character with octal value OOO (3,5) \xhh Character with hex value HH (4,5)
Notes¶
- Individual code units which form parts of a surrogate pair can be encoded using this escape sequence.
- Any Unicode character can be encoded this way, but characters outside the Basic Multilingual Plane (BMP) will be encoded using a surrogate pair if Python is compiled to use 16-bit code units (the default).
- As in Standard C, up to three octal digits are accepted.
- Unlike in Standard C, exactly two hex digits are required.
- In a string literal, hexadecimal and octal escapes denote the byte with the given value; it is not necessary that the byte encodes a character in the source character set. In a Unicode literal, these escapes denote a Unicode character with the given value.
Remarks¶
Unless an ‘r’ or ‘R’ prefix is present, escape sequences in strings are interpreted according to rules similar to those used by Standard C. Unlike Standard C, all unrecognized escape sequences are left in the string unchanged, i.e., the backslash is left in the string. (This behavior is useful when debugging: if an escape sequence is mistyped, the resulting output is more easily recognized as broken.) It is also important to note that the escape sequences marked as “(Unicode only)” in the table above fall into the category of unrecognized escapes for non-Unicode string literals.
When an ‘r’ or ‘R’ prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. For example, the string literal r”n” consists of two characters: a backslash and a lowercase ‘n’. String quotes can be escaped with a backslash, but the backslash remains in the string; for example, r”“” is a valid string literal consisting of two characters: a backslash and a double quote; r”” is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, NOT as a line continuation.
When an ‘r’ or ‘R’ prefix is used in conjunction with a ‘u’ or ‘U’ prefix, then the uXXXX and UXXXXXXXX escape sequences are processed while all other backslashes are left in the string. For example, the string literal ur”u0062n” consists of three Unicode characters: ‘LATIN SMALL LETTER B’, ‘REVERSE SOLIDUS’, and ‘LATIN SMALL LETTER N’. Backslashes can be escaped with a preceding backslash; however, both remain in the string. As a result, uXXXX escape sequences are only recognized when there are an odd number of backslashes.
Additionally, you can express any character by its numerical ASCII code by writing a backslash character \ followed by the ASCII code expressed as an octal (base-8) or hexadecimal (base-16) number. In the first case (octal) the digits must immediately follow the backslash (for example 23 or 40), in the second case (hexadecimal), an x character must be written before the digits themselves (for example x20 or x4A).
Example 1¶
>>> # this example writes a string "ABC" using hex >>> "\x41\x42\x43" 'ABC'
How to Escape Characters in a Python String?
Regex symbols do have a special meaning in Python. And often during printing the text as per our needs, it becomes strenuous work to let the compiler comprehend that we need to filter a text without considering the special meaning of the regex symbols.
Regex symbols can be anything like square brackets ( [ ] ), a dot ( . ), an asterisk ( * ), etc. So, what’s the upshot ? There are alternatives to escape the special meaning of these symbols. We’ll use escape string Python.
Here’s a list of some of the regex symbols with their special meanings :
Characters | Special Meanings |
---|---|
^ | Starts with |
$ | Ends with |
* | Zero or more occurrences in an object |
[ ] | A set of characters |
+ | One or more occurrences |
However, we need to have an insight into what happens when we don’t avoid the special meaning of the characters in a string. The given example shows it well when we are trying to get the content mentioned inside brackets in a string object.
But, it was supposed to provide only the content inside brackets, i.e. without using any alternatives. Since brackets have their special meaning , the re.findall() function considered the string object as a whole inside the brackets and returned the same.
What we need to need is to just remove the special meaning of brackets while passing the argument in the re.findall() function.
Of all the alternatives to be discussed to escape string Python, what’s needed is the use of a backslash inside the string along with regex symbols to avoid their special meaning. So, to avoid special meanings, we need to escape them with \ like \. or \- .
Let’s look at the following three methods to put a backslash to remove the special meanings from a string.
Method 1: Using re.escape()
The easiest way to avoid special meanings of the characters is the use of the re.escape() method which takes the pattern as an argument and returns the same with a double backslash before each of the regex symbols, be it a dot, an asterisk, square brackets, etc.
Below is the Python code making use of re.escape() :
escape\-string\-python\ with\ re\.escape\(\)
You may notice that the printed string is different here from its representation. The representation of the string contains double backslashes everywhere while there are single backslashes in the printed string. This is because the __repr()__ function is used to represent a string where a backslash is represented by \\ since it’s an escape character.
Method 2 : Using String Translate Table
Another way to escape string in Python is by using a translate table that returns a translated string based on the character translations that we define inside the table. The table comprises an object ( dict objects) where each of the character definitions is mapped to their character translations. Therefore, the special characters in the entire string will be converted to their respective character changes given in the table.
And in the table, we have just added a single backslash with each character and mapped them to their original characters given in the table. Since we need to define this function, we’ll name it translate_table .
It internally uses the str.maketrans() function that returns a mapping table that will be used by the str.translate() function that will finally replace the specified characters with their mapped values in the table.
The translate table will be defined in this manner :
We shall look at an example where a string with the regex symbols will be replaced with a backslash before each of the regex symbols.
escape\-string\-python with translate\-table
Method 3: Using Backlash
Finally, in a general scenario where we don’t need to work with huge string objects and just some small inputs, then it’s easy to provide a backslash to escape any special character.
Define a string that has special characters but without the backslash.
There is a syntax error in the output. As mentioned earlier in the article, special characters have meaning in Python. But these characters can still be used without their special meanings, just with a backslash before each one of them.
Examples for Understanding
Consider another example to properly understand the escaping of special characters in Python. We’ll use both the re.escape() function and the string translate table to return a modified string with a backslash.
Here’s the string object that will be used in further examples.
Using re.escape()
Using String Translate Table
Learn More
Here’s a lot more to learn more about String and modules in Python :
Conclusion
- To escape special characters in a Python string, we can use backslash, re.escape() , or a string translate table.
- The re.escape() function takes a pattern as input and returns a string with special characters escaped.
- String translate tables are created using str.maketrans() and map specific characters to new values.
- The resulting table is used with the str.translate() function to return a translated string.