Python

Python Escape Character

Escape characters or sequences are illegal characters for Python and never get printed as part of the output. When backslash is used in Python programming, it allows the program to escape the next characters.

Following would be the syntax for an escape sequence

Syntax:

\Escape character

Explanation:

Here, the escape character could be t, n, f, or backslash itself.

Types of Escape Sequence

Escape characters can be classified as non-printable characters when backslash precedes them. The print statements do not print escape characters.

Here is a list of Escape Characters with example usage

Escape characterDescriptionExample CodeResult
\nThe new line character helps the programmer to insert a new line before or after a string.str = ‘Hi\nTechpoints!’
print(str)
Hi
Techpoints!
\\This escape sequence allows the programmer to insert a backslash into the Python output.str = ‘Hi\\Techpoints!’
print(str)
Hi\Techpoints!
\xhhUse a backslash followed by a hexadecimal number.
This is done by printing in backslash with the hexadecimal equivalent in double quotes.
txt = ‘\x47\x75\x72\x75’ + ‘!’
print(txt)
HiTechpoints!
\oooTo get the integer value of an octal value, provide a backslash followed by ooo or octal number in double-quotes.
It is done by printing in a backslash with three octal equivalents in double quotes.
txt = ‘\107\125\122\125’+ ‘!’
print(txt)
HiTechpoints!
\bThis escape sequence provides backspace to the Python string. It is inserted by adding a backslash followed by “b”.
“b” here represents backslash. Removed ‘i’ letter
txt = ‘Hi\bTechpoints!’
print(txt)
HTechpoints!
\fIt helps in the interpolation of literal stringstxt = ‘Hi\fTechpoints!’
print(txt)
HiTechpoints!
\rIt helps you to create a raw stringtxt = ‘Hi\rTechpoints!’
print(txt)
Techpoints!
\’It helps you to add a single quotation to stringtxt = ‘Hi\’Techpoints!’
print(txt)
Hi’Techpoints!

What Does “\t” Do in Python?

The t alphabet in Python represents a space. It enables you to insert space or tab between strings in a code. It helps us to have space in the Python program when there is a need for it. To eliminate the usage of keyboard space, the coders utilize tab escape sequences.

Following is the syntax for a tab escape sequence.

Syntax:

"\t"

Example:

In this example, the string used is “HiTechpoints”. The program will put a tab or a space between Hi and Techpoints.

Code:

str = 'Hi\tTechpoints'
print(str)

Output:

Hi	Techpoints

Explanation:

In the above example, instead of adding space using a keyboard, the program helps us by putting a space or a tab between the string “HiTechpoints”. It also provides a space at the precise location where the escape sequence is added.

When to use “\t” in Python?

The escape sequence tab is utilized to put a horizontal tab between words and hence helps manipulate python strings. However, If the escape sequence tab is not used, the programmer has to manually add a space between every word of the string.

You can transform it into a time-consuming exercise. Moreover, the space added between different keywords may or may not be precise in its placement.

Here is an example that displays the manual addition of a space between words and the use of an escape sequence between words.

Code:

print('Manually Added  space in string Hi Techpoints')
str = 'Use\tof\ttab\tto\tadd\tspace\tHi\tTechpoints'
print(str)

Output:

Manually Added  space in string Hi Techpoints
Use	of	tab	to	add	space	Hi	Techpoints

Explanation:

The programmer manually added space between words in the above code, so the placement was not precise. When the escape sequence tab was applied, the program automatically provided the precise location of space between words.

Application of built-in function Chr () and Ord ()

The Chr () function is a built function that takes a single argument as input. The function takes Unicode characters as an input which ranges from 0 to 1,114 and 111, respectively. The function can be used as the substitute for escape sequence “\t” to put a space between two words.

The syntax for the Chr function is represented below: –

Syntax:

Chr(Unicode character)

The tab has the Unicode character 9. Use the following Python command to arrive at the Unicode character as shown below:

Code:

print('Unicode character of the tab is')
val = ord('\t')
print(val)

Output:

Unicode character of the tab is
9

Explanation:

The above code provides the Unicode character for the tab. It can be used as an input for the Chr function. Use of Chr (9) would allow us to create a substitute for a tab escape sequence.

This code is an example of how to use Chr (9), as shown below:

Code:

str = 'Hi' + chr(9) + 'Techpoints'
print(str)

Output:

Hi    Techpoints

The above function, however, is deprecated for version 3 and above.

Conclusion:

  1. Backslash is also regarded as a special character.
  2. To create an escape sequence, begin with a backslash followed by the illegal character.
  3. Examples of escape sequences include “\b”, “\t”,”\n”,”\xhh” and “\ooo” respectively.
  4. “\t” allows inserting a space or tab between two words. It plays a similar role to the space key present on the keyboard.
  5. “\t” is used when the programmer wants to add space to a string at a precise location.
  6. Certain whitespaces help in putting a new line between python strings.
  7. Line feed and carriage return, vertical tab, and form feed are types of whitespaces.

About the Author: John Henry

I am python developer. Happy to share what I know