Escape Characters in Python | Explained with Examples

In any programming language, we have to deal with different data types such as string, list, tuple, etc. The string is used to input any characters enclosed in double or single quotation marks.

Some special characters give errors when they are put directly into the strings. To handle such errors, the escape characters are used, which helps us to enter illegal characters inside the string. This post will provide a deep insight into the Python “Escape Characters” with the help of suitable examples.

So, let’s get started!

What are Escape Characters in Python?

In Python, the backslash character “\” is an escape character. An escape character is used with the combination of backslash “\” and a special character such as for newline “\n” combination is used

Every escape character performs some specific operation when used inside the string.

We can insert single or double quotes, add new lines, add whitespaces, etc inside the string.

Let’s learn different escape characters with their example one by one:

Example 1: Add a Single Quote in a String

In Python, the preceding backslash “\” is used along with the single quote escape character to add a “single quote” inside the string or substring. If we directly put the quote character on any string, the output will give us an invalid syntax error. Let’s understand via the following example:

Code

Value = 'Good\'Morning'
print(Value)

In the following code, the “preceding backslash” is used to add the “single quote” in the string value stored in a variable named “value”.

Output

 The “single quote” is added at the start of the substring named “Morning”.

Example 2: Add a Double Quote in a String

The “Double Quote” is also placed inside the string using a preceding backslash. Let’s do it by the following code:

Code

Value = "hello\"world"
print(Value)

In the above code, the “preceding backslash” is used to add the “double quote” in the substring of the string value that is stored in a variable named “value”.

Output

The substring named “world” has a “double quote” at the start.

Example 3: Add a Backslash in a String

The “backslash” is added inside the string using double backslash “\\”.Similarly, to add a double backslash we need to use three preceding backslashes “\\\” inside the string. Let’s see an example of code shown below:

Code:

Value = 'hello\\world'
Value_1 = 'hello\\\world'
print(Value)
print(Value_1)

In the above lines of code, the two backslashes “\\” are used inside the string value of the first variable to add one backslash “\” before the substring named “world”.simalrly three backslashes “\\\” is used to add two backslashes”\\” inside the string.

Output:

From the output, single and double backslash “\” is added before the substring “world”.

Example 4: Add a New Line to a String

The newline character “\n” is used to add the newline at any position in the string. When this character is used in the string, the sequence is terminated and shifted to the next line. Let’s see an example of code given below:

Code:

Value = 'Good\nMorning'
print(Value)

In the above code, a newline character “\n” is used before the substring named “morning”. This character is added by placing the preceding backslash with the character value “n”.

Output:

The substring “Morning” is positioned at the new line.

Example 5: Remove Specific Characters in a String

The “carriage return” has a special character with preceding backslash “\r” that will return the content of the string used after the character on output. The content of the string before the escape character “\r” will be removed. Let’s understand it with the code given below:

Code:

value = 'its\rlinuxfoss'
print(value)

In the above code, the preceding backslash is used with the character “r” to remove all the string values before its position. Only the value after the carriage return character will be left.

Output:

The output shows that the substring “its” has been removed, and only “linuxfoss” is left.

Example 6: Add Multiple White Spaces in a String (Tab Key)

The “tab” escape gives the tab space between the string that is equal to “4” simple spaces. The tab escape is also used with preceding backslash with the special character “t”. Let’s understand via the following code:

Code:

value = 'its\tlinuxfoss'
print(value)

The “Tab” character is used along with preceding backslash such as “\t”. This character “\t” generates a tab space between the substring value “its” and “linuxfoss”.

Output:

The output shows the space between the two substrings.

Example 7: Remove a Single Character in a String

The “Backspace” escape deletes the previous character of the string. It only deletes one previous character at a time. The backspace escape character “b” is used along with preceding backslash like “\b”. Let’s understand it with an example of code:

Code:

value = 'its\blinuxfoss'
print(value)

In the following code, the backspace character is used along with the preceding backslash “\b”. The “\b” escape character deletes the value of its last previous character “s” from the substring.

Output:

The output shows that the character “s” is removed from the string “itslinuxfoss”.

Example 8: Represent an Octal Value to its Equivalent

Octal number system ranging from “0” to “7”. This means the value can not be more than the “7”. In Python, every octal value has a special character value; for example, character “A” is “101” in octal representation. Every character has a specific octal value that is predefined in Python. Let’s see an example of code given below:

Code

value = '\101\102\103\104\105\106'
print(value)

The octal value is used to generate the alphabetic character using the specific number. Every value has a specific octal number like the character “A” has an octal value of “101”. All octal numbers are placed inside the string using the preceding backslash.

Output

The output shows the character values “A”,” B”, and” CDEF” on the screen.

Example 9: Represent a Hexa Value to its Equivalent

In Python, Hexadecimal values are converted into special Characters. The character value is printed on the basis of their hex value like the hex value of “A” is “x41” these are Python-defined terms that are different for each character. Let’s convert the hex value into character using the following code:

Code

value = '\x41\x42\x43\x44\x45\x46'
print(value)

In the above code, the “Hex” values are specified using the preceding backslash with the character “x” and two number digits. The “x” character is mandatory to use because it specifies the hex type value.

Output

The character value “ABCDEF” is shown on the console screen.

That’s it from this informative guide!

Conclusion

In Python, Escape characters such as “single quote”, “double quote ”, “newline”, “carriage return”, “tab” etc are used to place characters inside the string. The escape characters are placed inside the string using preceding backslashes such as “\b” for Backspace. Every escape character inside the string has a specific purpose and to complete that purpose we have to place the escape character before or after the substring of the string. This Python guide has provided detailed working and usage of the escape characters in Python.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×