Python Escape Sequence Interpretation

Python Escape Sequence interpretation is done, when a backslash is encountered within a string. After the encounter of a backslash (inside a string), any following character (with the ( \ )) would be looked upon in the aforementioned table. If a match is found then the sequence is omitted from the string, and its translation associated with the sequence is used. If a match is not found, then no lookup happens, and the control sequence is copied as it is. 

Example

Python3




# A string with a recognized escape sequence
print("I will go\tHome")
 
# A string with a unrecognized escape sequence
print("See you\jtommorow")


Output:

As seen in the below output, the first print statement produced an output where the \t got resolved into a vertical tab and is omitted in the output. On the other hand, in the second print statement, the \j persists, as no legal resolution for that sequence exists.

I will go    Home
See you\jtommorow

Problems due to escape characters may not always result in undesirable output, but also errors. For example, the below code upon execution will produce an error.

Python3




print("C:\Users\Desktop\JSON")


Output:

Produces the following error

print("C:\Users\Desktop\JSON") 
       ^ 
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

The error is caused because the  \U  in the string leads to the next 4 characters being treated as a 32-bit Hexadecimal value which would correspond to a Unicode code point. This leads to an error as the next character is which is outside the base 16 range. 

Preventing Escape Sequence Interpretation in Python

Python Escape Sequence is a combination of characters (usually prefixed with an escape character), that has a non-literal character interpretation such that, the character’s sequences which are considered as an escape sequence have a meaning other than the literal characters contained therein. Most Programming languages use a backslash \ as an escape character. This character is used as an escape sequence initiator, any character (one or more) following this is interpreted as an escape sequence. If an escape sequence is designated to a Non-Printable Character or a Control Code, then the sequence is called a control character. 

Similar Reads

List of Escape Sequence in Python

Escape Character Meaning \’ Single quote \” Double quote \\ backslash \n  New line \r Carriage Return \t Horizontal tab \b Backspace \f form feed \v vertical tab \0 Null character \N{name} Unicode Character Database named Lookup \uxxxxxxxx Unicode Character with 16-bit hex value XXXX \Uxxxxxxxx Unicode Character with 32-bit hex value XXXXXXXX \ooo Character with octal value OOO \xhh Character with hex value HH...

Python Escape Sequence Interpretation

Python Escape Sequence interpretation is done, when a backslash is encountered within a string. After the encounter of a backslash (inside a string), any following character (with the ( \ )) would be looked upon in the aforementioned table. If a match is found then the sequence is omitted from the string, and its translation associated with the sequence is used. If a match is not found, then no lookup happens, and the control sequence is copied as it is....

Preventing Escape Sequence Interpretation in Python

...

Contact Us