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 \.
Following table provides the list of escape characters in Python.
Single Quote Escape Character
To escape single quote character, use a preceding backslash for the single quote in the string.
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.
Double Quote Escape Character
To escape double quote character, use a preceding backslash for the double quote in the string.
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.
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.
Newline Escape Character
To escape newline character, use a preceding backslash for character ‘n’ in the string.
Carriage Return Escape Character
To escape carriage return character, use a preceding backslash for character ‘r’ in the string.
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.
Backspace Escape Character
To escape backspace character, use a preceding backslash for character ‘b’ in the string.
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.
Octal Value Escape Character
To escape a byte of octal value character, use a preceding backslash for three digit octal value in the string.
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.
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.
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.