We know an excellent deal about print function now. With this knowledge, let's try to print "ToolsQA" - Sounds simple? We know we can print anything using a print function with a double-quote inside. So what if we need to print something which already has a double quote? Can we use multiple double quotes in this case? Something like print (" "ToolsQA" ")? No, it doesn't work, unfortunately. This is where escape sequences in Python come for our rescue.
Additionally, we will also get to know some other fantastic stuff. Here is what we will cover in this tutorial
- What is an Escape character?
- Common Escape Sequences in Python
What is an Escape Character?
The programmers refer to the "backslash (\)" character as an escape character. In other words, it has a special meaning when we use it inside the strings. As the name suggests, the escape character escapes the characters in a string for a brief moment to introduce unique inclusion. That is to say; backlash signifies that the next character after it has a different meaning. It could be printing double quotes or indicating a new line. Let's see what escape sequences are and how to use them in Escape Sequences in Python.
Common Escape Sequences in Python
The sequence of characters after a backslash is known as an escape sequence. Moreover, in Python, we have different escape sequences that have a unique meaning. Let's see the most common examples of escape sequences.
\" and \'
We have tried to print "ToolsQA" earlier using print(""ToolsQA"") statement, and it didn't work !! Why did we get an error? It's because of the quotes that we have used in the text. We have used double quotes to close the text and, at the same time, used double quotes inside the outer pair of double-quotes.
Consequently, it confuses the interpreter. In other words, the interpreter accepts only one pair of quotes of a similar type. But, in our example, we have a double pair of the same kinds of quotes.
That's the reason why we get an error. Consequently, run the same example and see the output.
# wrong quotes
print(""ToolsQA"")
How to resolve our problem? We can do it in several ways. As we are talking about the escape character, let's see how to use it to get rid off the error. Put a backslash before the quote that you wish to have in your text. Let's run the code below.
# double-quotes inside a doubled quotes
print("\"ToolsQA\"")
As you see, we didn't get any errors. What happening actually? The escape character gives a new behavior to the character, i.e., a double quote (") next to it. In other words, we have used the double quote and backslash in combination (\
"). That combination is one of the escape sequences in Python. This escape sequence tells Python that it needs to remove the backslash and put the quote in the string.
Another escape sequence \'
similar to the first escape sequence \"
. I will explain it briefly.
\'
is used in the same context as we did for the ". That is to say; it prints the single-quotes. Let's try one example using the single-quotes. Print the text Hi, I'm from ToolsQA using single quotes as closing quotes. Moreover, if we try it as a usual print statement without any escape character, then we will get the error. Let's see the error.
# wrong pair of quotes
print('Hi, I'm from ToolsQA')
I think you would have figured out the solution. If not, then see the code below.
# using \ to resolve the problem
print('Hi, I\'m from ToolsQA')
Like with double quotes, we didn't get any error here as well.
\ - Double Backslash Escape Sequence
We know how to print double quotes and single quotes but what if we need to print the backslash () itself? From what we have learned so far, we will probably write the below code. Therefore, let's try to run it.
# trying to print \
print("\")
Python throws an error. When you write \,
Python expects an escape sequence. But there is no escape sequence in the text.
Consequently, we got an error. To print a backslash to the console, we have to use the double backslashes (\\
). Let's try it.
# trying to print \
print("\\")
\n - New Line Escape Sequence
\n means a new line. We will get a new line when we use the escape sequence \n in the print function. Let's see what happens when we put the escape sequence \n in the middle of the text.
# \n usage
print("I am from\nToolsQA")
Can you guess the output? I think most of you already guessed it.
\t - Tab Space Escape Sequence
It gives a tab space in the output. Let say we have a text "I am from\tToolsQA", then the output will contain a tab space between the words from and ToolsQA. Additionally, let's see the example in IDE.
# \t usage
print("I am from\tToolsQA")
Do we have any other escape sequences in Python? Yeah! We have some more escape sequences. Let's see the summary of these escape sequences in a tabular form. These are quite simple. Additionally, we would encourage you to try these on your own.
Escape Sequence | Usage |
---|---|
\v | It prints the text next to it in a newline after a tab space. Additionally, try the example print("Tools\vQA") |
\b | It removes the character before \b. Moreover, the statement print("ToolsQA!\b") prints ToolsQA. |
\r | It moves the cursor to the beginning of the line. In addition to this, it's called carriage return. Try the example print("Hello\rToolsQA"). |
(octal) | It converts the octal value to the corresponding ASCII character. Moreover, the statement print("\124") prints the letter T, which is the ASCII character of the number 0o124 = 84. |
\x(hexadecimal) | It converts the hexadecimal value to the corresponding ASCII character. Additionally, the hexadecimal value must start with the x character. The statement print("\x54") prints the letter T |
Key Takeaways
- \ is the escape character in Python. Additionally, it announces that the next character has a different meaning.
- Moreover, the set of characters after , including it, is known as an escape sequence.
\
" and\
' prints double and single quote respectively.- We need to use a double backslash (\) to print a backslash () as \ is an escape character.
- Additionally, \n outputs a new line.
- Similarly, \t inserts a tab space.
- \v takes the text after it to the next line with a tab space before it.
- In the same vein, (octal) converts into the corresponding ASCII character.
- Additionally, \x(hexadecimal) prints the corresponding ASCII character similar to the above one.
Python Escape Sequence - Practice Exercises
#Problem 1
print("ToolsQA is an \"awesome\" website.")
#Problem 2
print("ToolsQA\n\t2020")
#Problem 3
print('I\'m from ToolsQA.\b')
#Problem 4
print("\65")
#Problem 5
print("\x65")
#Problem 6
print("ToolsQA", "2020", sep="\n")
#Problem 7
print("ToolsQA", "2020", sep="\b")
#Problem 8
print("ToolsQA", "2020", sep="*", end="\b\b\b\b")
What Next?
To conclude, we have learned about the escape character and sequences in the tutorial. And it's not the end. We can use them in different use cases as well. Subsequently, we will see them in the upcoming tutorials.
In the next tutorial, we will see the most used topics of a programming language, i.e., literals and data types. Moreover, those are the building blocks of a programming language. Therefore, stay focused on the course. Don't forget the solve the problems from the last section.
Happy Coding :)