- Escape Sequence in Python
- Escape Sequence in Python
- Common Escape Sequences in Python
- Escape Sequence in Python with examples
- Escape single quotes in Python
- Python escape sequence n
- Escape sequence backslash in Python
- Escape sequence for space in Python
- Escape sequence backspace in Python
- Escape sequence for Hexa value in Python
- Escape sequence for Octal value in Python
- Escape sequence for space in Python
- Raw Strings and Escape Sequences in Python
- Conclusion
- Escape Backslash Character in Python
- How to use escape backslash character in python?
- Using the escape backslash character in python to represent whitespace characters.
- Using the escape backslash character in python to turn special characters into ordinary characters.
- Escape Sequences in Python
- Raw strings
- Common escape sequences
Escape Sequence in Python
Escape sequences in Python are used to represent certain special characters within string literals. This helps to include characters that can’t be directly included in a string, such as a newline, tab, or non-printable characters. In Python, escape sequences are prefixed with a backslash (). In this tutorial, we will discuss various escape sequences in Python, and how you can use them.
Escape Sequence in Python
An escape sequence is a special character used in the form of backslash(\) followed by a character that is required. These characters are used to represent whitespace. Whitespace gives characters like space, tab, formfeed, and vertical tab.
Common Escape Sequences in Python
Here is a list of common escape sequences in Python:
Escape Sequence | Description |
---|---|
\\ | Backslash ( \ ) |
\’ | Single quote ( ‘ ) |
\» | Double quote ( » ) |
\n | Newline |
\t | Tab |
\r | Carriage return |
\b | Backspace |
\f | Form feed |
\ooo | Character with octal value ooo |
\xhh | Character with hex value hh |
Escape Sequence in Python with examples
Let us check out a few examples of escape sequences in Python.
Escape single quotes in Python
Let us check an example of escape single quotes in Python, we can see how to use \’ single quote in strings in Python.
string = 'That\'s my bag.' print(string)
The below screenshot shows that a single quote is used for the word That’s.
Python escape sequence n
Let us see an example of Python escape sequence n. I have used a “\n” newline character. A newline character is used to write the words in a new separate line.
string = "python\n guides" print(string)
You can refer to the below screenshot for the output, you can see the words in a new separate line.
Escape sequence backslash in Python
Let us check an example of a Python escape sequence backslash. The backslash is an escape sequence, \\ is used to print a single backslash.
string = "python\\ guides" print(string)
You can refer to the below screenshot for the output:
Escape sequence for space in Python
In this example, I have used “\t” character to get space between the words.
string = "python\tguides" print(string)
You can refer to the below screenshot for the output, we can see the space between the word python and guides.
Escape sequence backspace in Python
In this example, I have used “\b” to remove the space between the words in Python.
string = "python \bguides" print(string)
You can see the output in the below screenshot.
Escape sequence for Hexa value in Python
Let us check an example of a Python escape sequence for Hexa value, I have used \xhh to convert hexa value into a string.
string = "\x50\x59\x54\x48\x4f\x4E \x47\x55\x49\x44\x45\x53" print(string)
In the below screenshot, we can see the converted string.
Escape sequence for Octal value in Python
Let us check an example of a Python escape sequence for Octal value, I have used \ooo to convert the Octal value into a normal string.
string = "\120\131\124\110\117\116 \107\125\111\104\105\123" print(string)
You can refer to the below screenshot for the output:
Escape sequence for space in Python
Let us see an example of an escape sequence for space in Python.
In this example, I have used \t between the words to get space.
string = "python\tguides" print(string)
In this output, we can see the space between the words.
Raw Strings and Escape Sequences in Python
In Python, you can create raw strings by prefixing the string with the letter r or R. In raw strings, backslashes are treated as literal characters and not as escape characters in Python.
raw_string = r"This is a raw string\nThis will not be on a new line" print(raw_string)
Conclusion
I hope you got an idea on escape sequence in Python also we saw, escape sequence in Python with examples.
You may like the following tutorials:
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.
Escape Backslash Character in Python
The backslash character holds a special place among all the other characters in the world of Python programming. It has multiple uses in Python, although all of them are quite similar to each other. This article demonstrates what is the backslash character and how it can be used in Python.
What is a backslash character in Python?
A backslash character can be classified as a unique character in Python that is essential in representing a couple of whitespace characters. It does that by combining itself with certain normal characters.
An example of this would be the \n whitespace character that specifies a newline character. It can also be utilized to convert other special characters to normal characters. Therefore, it is also called an escape character.
How to use escape backslash character in python?
Using the escape backslash character in python to represent whitespace characters.
As defined above, when combined with certain specific ordinary characters, it turns into a character that specifies some condition, which represents a whitespace character. The \n newline character, the \t tab character, and the \r carriage return character are a few examples of these characters.
The following code uses the escape backslash character to represent whitespace characters.
The above code provides the following output:
Here, we explain the use of a backslash character to represent a whitespace character in the code, namely the \t tab character.
Using the escape backslash character in python to turn special characters into ordinary characters.
The backslash is a fundamental part of the reverse process of the above as well. When a special character is prefixed by the backslash character, it can be seen as becoming an ordinary character. This is where it gets the name escape character, as it is capable of making the special characters escape the compiler’s reach and be passed as ordinary characters.
To make things more clear, we will be making use of the apostrophe character ‘ , which in itself is a special character and cannot be normally displayed as a part of a string. This is because an apostrophe can also be deemed as a single quote character.
The following code uses the escape backslash character in python to turn special characters into ordinary characters.
Escape Sequences in Python
Escape sequences allow you to include special characters in strings. To do this, simply add a backslash ( \ ) before the character you want to escape.
For example, imagine you initialized a string with single quotes:
But if you include an apostrophe without escaping it, then you will get an error:
File "main.py", line 1 s = 'Hey, what's up?' ^ SyntaxError: invalid syntax
To fix this, just escape the apostrophe:
To add newlines to your string, use \n :
print("Multiline strings\ncan be created\nusing escape sequences.")
Multiline strings can be created using escape sequences.
An important thing to remember is that, if you want to include a backslash character in a string, you will need to escape that. For example, if you want to print a directory path in Windows, you’ll need to escape each backslash in the string:
Raw strings
A raw string can be used by prefixing the string with r or R , which allows for backslashes to be included without the need to escape them. For example:
print(r"Backslashes \ don't need to be escaped in raw strings.")
Backslashes \ don't need to be escaped in raw strings.
But keep in mind that unescaped backslashes at the end of a raw string will cause and error:
print(r"There's an unescaped backslash at the end of this string\")
File "main.py", line 1 print(r"There's an unescaped backslash at the end of this string\") ^ SyntaxError: EOL while scanning string literal
Common escape sequences
Escape Sequence | Meaning |
---|---|
\ | Backslash ( \ ) |
‘ | Single quote ( ‘ ) |
« | Double quote ( » ) |
\n | ASCII Linefeed (adds newline) |
\b | ASCII Backspace |
A full list of escape sequences can be found here in the Python docs.
If this article was helpful, tweet it .
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.