Python Escape Sequence or Escape Characters
An escape sequence is a sequence of characters with special meaning when used inside a string. By using escape characters, we can modify the result that, in a normal way, we can’t. We can insert illegal characters or modifications in the result that are normally not allowed in Python.
Another definition: “Escape sequence is a sequence of characters, which we can use inside a string. These escape characters do not represent themselves but are converted into another character or series of characters that may be difficult or impossible to express directly.”
Note: escape characters or escape sequence is the same. In this tutorial, we are going to use both names.
Syntax for Python Escape Sequence
In Python, the backslash (\) is used to represent an escape character. It means the character we used after the backslash is an escape character.
Syntax: \n |
Now here first we write backslash and after that n character (\n). Remember, there is no space between blackslash and n character. Blackslash is mandotry; after that, we write character name. Here we write n character. So \n is an escape character that is used to create a new line.
Now remember backslash(\) is used to write an escape character in Python. So basically, whenever we write a backslash, interpreters assume we’re going to write an escape character after the backslash.
Note: Always remember, escape characters do not get printed in the output.
Types of Escape Characters in Python
There are several kinds of escape characters in Python:
Single backslash (\) | Double backslashes (\\) | Single quotes (\’) | Double quotes (\”) | New line (\n) |
Tab (\t) | Backspace (\b) | Carriage return (\r) | Octal value (\ooo) | Hexadecimal Values (\xhh) |
Single backslash (\)
Now in the upper topic, we learned whenever we want to write an escape character, we use a backslash. Now here we’re going to see the output when we used only a backslash in our progaram.
Code:
text = 'Hello \Python'
print(text)
Output:
SyntaxWarning: invalid escape sequence '\P'
We get an error with this message: ‘invalid escape sequence \P’. The point is, we are not going to use any escape sequence; we simply use a backslash in text. But as we learn in the syntax, as soon as we write a backslash, the interpreter assumes we are going to use an escape character. And in this text, Hello \Python
we use P just after the backslash, so the interpreter assumes that we want to use \P
as an escape sequence. But \P is not an escape character.
If we write this code like:
Code:
text = 'Hello \ Python'
print(text)
Output:
SyntaxWarning: invalid escape sequence '\ '
We get the same error. Now we use a space after a backslash; ('\ ')
now the interpreter assumes we are going to use an escape character but it is also not an escape character.
So a backslash is used to represent escape characters. Whenever we use a backslash, the interpreter assumes we want to use an escape character.
However, a single backslash has a work if we are working with multiline strings. Normally, when we write triple quoted strings, the result comes in multlines. For example:
Code:
text = '''Hello Python,
This is a program.
I am learning programming.
Python is good language.'''
print(text)
Output:
Hello Python,
This is a program.
I am learning programming.
Python is good language.
Now if we use a backslash at the end of each line, the output comes in a single line.
Code:
text = '''Hello Python,\
This is a program.\
I am learning programming.\
Python is good language.'''
print(text)
Output:
Hello Python,This is a program.I am learning programming.Python is good language.
So in a normal way, a backslash is used for an escape sequence. But if we used backslashes in multiple lines, then the interpreter ignored the newlines and we got the output in a single line.
Double backslashes (\\)
What if we want to print a backslash in the output? Like, if we want the text, 'Come here\go there'
in the output. How are we going to write it?
Code:
text = 'Come here\go there'
print(text)
Output:
SyntaxWarning: invalid escape sequence '\g'
We already learned in the upper example that we cannot do this. So if we want to backslash in the output, we have to use double backslashes.
Code:
text = 'Come here\\go there'
print(text)
Output:
Come here\go there
So we got the desired output. Here, when we use a backslash for the first time, the interpreter assumes we are going to use an escape character. So we use another backslash after the first backslash, which means if we use a single backslash after the backslash, it works as escape character.
We can use two or more time double backslashes if we want.
Code:
text = 'Come here\\go there, again come here\\go there'
print(text)
Output:
Come here\go there, again come here\go there
If we want two times backslashes in the output.
Code:
text = 'Come here\\\\go there'
print(text)
Output:
Come here\\go there
It’s easy to understand. First double backslashes produce a single backslash in the output. Then again, double backslashes produce another single backslash in the output.
Single quotes (\’)
Just like in the upper example, when we use a backslash after the backslash, we get a backslash in the output. Same if we use a single quote after the backslash; we get a single quote in the output. For example, if we want to print the text, It's Raining
if we simply write our code like this:
Code:
text = 'It's Raining'
print(text)
Output:
SyntaxError: unterminated string literal
We get an error. We cannot use a single quote in a string that already has a single quote character inside. This is the basic rule for using quotes in Python. However, if we changed single quotes into double quotes, we could get the desired output.
Code:
text = "It's Raining"
print(text)
Output:
It's Raining
We get the desired output but we didn’t use any escape sequence. So how to do this with an escape sequence? We simply put a backslash just before the quote sign.
Code:
text = 'It\'s Raining'
print(text)
Output:
It's Raining
We simply put a blackslash before the quote sign and now the interpreter assumes we want to use the escape sequence. So single quote with backslash (\’) is an escape character that is used to print single quote (‘) in the output.
Now what if we want multiple times, single quotes in the output?
Code:
text = 'It\'s Raining, It\'s continue raining'
print(text)
Output:
It's Raining, It's continue raining
We simply put a backslash before the single quote to get the desired output.
Click to learn for Python quotes tutorial
Double quotes (\”)
Just like in the upper example, we used single quotes with backslashes. We can use a backslash with double quotes to get the double quotes in the output. For example, if we want the text, He is "Mr. Nobody" in this house.
If we write out code like this:
Code:
text = "He is "Mr. Nobody" in this house."
print(text)
Output:
SyntaxError: invalid syntax
It’s easy to understand that we can’t get the desired output if our strings are already double quoted. So here we are going to use an escape sequence. We are simply going to put a backslash before the double quote characters, which we want in the output.
Code:
text = "He is \"Mr. Nobody\" in this house."
print(text)
Output:
He is "Mr. Nobody" in this house.
We simply put a backslash before the double quote and get the desired output.
New line (\n)
If we use a backslash with character n, (\n). Now this is an escape character in Python. It is used to create a new line in Python. We can break a single line into multiple lines by using \n
. For example:
Code:
text1 = 'Hello I am learning Python.'
text2 = 'Hello \nI am \nlearning Python.'
print(text1)
print(text2)
Output:
Hello I am learning Python.
Hello
I am
learning Python.
The output from the text1
comes into a single line. And the output from the text2
comes into multiple lines. We simply used \n
just before the character where we wanted to break our line.
Another example: If we use \n
in the end of any strings, the next strings will print after a blank line.
Code:
print('FirstLine \n')
print('SecondLine')
Output:
FirstLine
SecondLine
Here we get a blank line between both outputs because we use \n
in the end of the first statement. If we use two time \n
, we will get two blank lines in the output.
Code:
print('FirstLine \n\n')
print('SecondLine')
Output:
FirstLine
SecondLine
So this is how we use \n
escape character for a new line in Python.
End of the Python Escape Sequence (Part 1) Tutorial
3 thoughts on “Python Escape Sequence (Part 1): What Are The Escape Characters In Python?”