Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Escape Sequences in Python

Escape sequences can be defined as the special characters that can be used or provided to insert characters that are not viable to be typed by the user. To insert a new line, an escape sequence can be used for the same as doing the task manually can not be efficient or even possible.

How to Use Escape Sequences

Escape sequences in python can be used by placing a backslash character and then the character that needs to be inserted. We already discussed in the previous section about inserting a new line, similarly in python, inserting double quotes inside a string can result in an error so using a backslash before the character can make it work.

Some of the escape sequences that are widely used by programmers are as follows:-

1. Single Quote (\’)

To use a single quote inside a string, a backslash followed by a single quote makes it valid to be used in a statement.

Example of Simple Quote:

string = "This is a string"
# Insert a single quote before the word "string"
newStr = string[:10] + '\'' + string[10:]
print(newStr)

Output:

This is a 'string

Explanation:
In the above program, we first define a string variable string. Then, we use string slicing to insert a single quote before the word "string". We define a new string variable newStr and use string concatenation to join the sliced parts of the original string and the single quote. Finally, we print the new string.

2. Backslash (\)

To use a backslash inside a string, a backslash must be written as usual before the backslash is printed.

Example of Backslash:

print(" Printed string is \\")

Output:

Printed string is \

Explanation:
In Python, the backslash is used as an escape character, so if you want to print an actual backslash, you need to use a double backslash \ or a raw string with an ‘r’ prefix like r"\"

3. Newline (\n)

Going to a new line is not possible by putting a bunch of pre-determined spaces in python. To go to a new line in Python, you can use the newline character \n within a string. When you print a string that contains the newline character, the output will be split into separate lines at the position of the newline character.

Example of Newline:-

print("This is the first line.\nThis is the second line.")

Output:

This is the first line.
This is the second line.

Explanation:
The \n character is used to indicate a newline, so when you run this program, the output will be both statements separated in two lines by \n character.

4. Carriage Return

It is one of the special escape sequences in python that lets us return to the start of the line overwriting whatever was already written. It is denoted by ‘r’ followed by a backslash.

Example Carriage Return:

print("This is the first line.", end="\r")
print("This is the second line.")

Output:

This is the second line.

Explanation:
The carriage will return to the start of the line at the end of the first statement and the second line will be printed in that case.

5. Tab

A Tab equals eight whitespaces and it can be implemented by writing backslash followed up by t. It is helpful in separating statements to improve the readability of string.

Example of Tab:

print("First\tSecond\tThird")
print("1\t2\t3")
print("A\tB\tC")

Output:

First   Second  Third
  1           2           3
  A           B          C

Explanation:
We wrote three print statements on each line and you can see, the output has three columns of text, with each column separated by a tab character. The \t character moves the cursor to the next tab stop, which is usually set to every eight characters by default.

6. Backspace

When the \b escape sequence is used within a string, it moves the cursor one position to the left, as a matter of fact, it erases the character that was previously present at the position.

Example Backspace:

print("abc\bdef")

Output:

abdef

Explanation:
From the above output, we can find that the backspace escape sequence in python can be used to erase the last typed character in the string, Here, the character was c, which was erased.

7. Octal Value

There exists an escape sequence in python that can convert the octal value into a string. It is represented by ‘\ooo’ where ‘ooo’ is a sequence that has 3 values each ranging from 0 to 7. Let’s take an example to understand.

Example of Octal Value:

print("Example of using the \141 escape sequence.")

Output:

Example of using the escape sequence.

Explanation:
In this above-mentioned code, the print() function is being used to print a string that contains the \141 as an escape sequence. When the escape sequence is read, Python replaces the corresponding ASCII character with the octal value 141, that is ‘a’.

8. Hexadecimal Value

There exists an escape sequence in python that can convert the hexadecimal value into a string. It is represented by ‘\xhh’ where ‘xhh’ is a sequence that has 2 values each ranging from 0 to 9 and A to F. Let’s take an example to understand this better.

Example of Hexadecimal Value:

print("Example of using the \x41 escape sequence.")

Output:

Example of using the A escape sequence.

Explanation:
In this above-mentioned code, the print() function is being used to print a string that contains the \x41 as an escape sequence. When the escape sequence is read, Python replaces the corresponding ASCII character with the hexadecimal value x41, that is ‘A’.

Conclusion
In this article, we studied about the various escape sequences in python that can be used to insert a character in our string. Escape sequences are an important part of formatting and also that raw strings permit backslashes. We studied various escape sequences in python and also how we can perform them in our code.

We hope to see you with another informative article here at PrepBytes soon.

Frequently Asked Questions

1. What is an escape sequence in Python?
An escape sequence in Python is a combination of characters that represents a special character or character sequence. An escape sequence starts with a backslash () followed by one or more characters that have a special meaning.

2. What are some common escape sequences in Python?
Some common escape sequences in Python include \n for a newline character, \t for a tab character, \r for a carriage return, \ for a backslash character, \" for a double quote character, and \’ for a single quote character.

3. Can I use escape sequences in Python strings and regular expressions?
Yes, you can use escape sequences in both Python strings and regular expressions. In strings, escape sequences are used to insert special characters or character sequences, while in regular expressions, escape sequences are used to match special characters or character sequences.

4. What should I do if I need to include a backslash in a Python string?
If you need to include a backslash character in a Python string, you can use the \ escape sequence to insert a literal backslash. For example, if you want to print the string C:\Users, you can use the following code: print("C:\Users").

Leave a Reply

Your email address will not be published. Required fields are marked *