Python Basics4
Python Basics4
Escape characters or sequences are illegal characters for Python and never get
printed as part of the output. When backslash is used in Python programming, it
allows the program to escape the next characters.
Syntax:
\Escape character
Explanation:
Here, the escape character could be t, n, e, or backslash itself.
Table of Contents:
Code Description
\’ Single quotation
\\ Backslash
https://www.guru99.com/python-escape-characters.html 1/10
12/13/24, 11:29 PM Python Escape Character Sequences (Examples)
Code Description
\n New Line
\r Carriage return
\t Tab
\b Backspace
\f Form feed
https://www.guru99.com/python-escape-characters.html 2/10
12/13/24, 11:29 PM Python Escape Character Sequences (Examples)
Escape
character Function Example Code Result
in double-quotes. “99!”
It is done by printing in a print(txt)
backslash with three octal
equivalents in double quotes.
RELATED ARTICLES
→ PyUnit Tutorial: Python Unit Testing Framework (with Example)
→ Python Rename File and Directory using os.rename()
→ Swap two numbers without using a third variable: C, Python Program
→ How to Check Python Version on Linux, Mac & Windows
https://www.guru99.com/python-escape-characters.html 3/10
12/13/24, 11:29 PM Python Escape Character Sequences (Examples)
there is a need for it. To eliminate the usage of keyboard space, the coders utilize
tab escape sequences.
Syntax:
“\t”
Example:
In this example, the string used is “Guru99”. The program will put a tab or a space
between Guru and 99.
Python Code:
This code is editable. Click Run to Execute
1 TextExample="Guru\t99"
2 print (TextExample)
3
Run
Output:
https://www.guru99.com/python-escape-characters.html 4/10
12/13/24, 11:29 PM Python Escape Character Sequences (Examples)
Guru 99
Explanation:
In the above example, instead of adding space using a keyboard, the program helps
us by putting a space or a tab between the string “Guru99”. It also provides a space
at the precise location where the escape sequence is added.
You can transform it into a time-consuming exercise. Moreover, the space added
between different keywords may or may not be precise in its placement.
Here is an example that displays the manual addition of a space between words
and the use of an escape sequence between words.
Python Code:
This code is editable. Click Run to Execute
https://www.guru99.com/python-escape-characters.html 5/10
12/13/24, 11:29 PM Python Escape Character Sequences (Examples)
Run
Output:
Explanation:
The programmer manually added space between words in the above code, so the
placement was not precise. When the escape sequence tab was applied, the
program automatically provided the precise location of space between words.
Syntax: –
Chr(Unicode character)
The tab has the Unicode character 9. Use the following Python command to arrive
at the Unicode character as shown below: –
Python Code:
This code is editable. Click Run to Execute
2 Ord=ord('\t')
3 print(Ord)
4
Run
Output:
Explanation:
The above code provides the Unicode character for the tab. It can be used as an
input for the Chr function. Use of Chr (9) would allow us to create a substitute for a
tab escape sequence.
Python Code:
TextExample="Guru+chr(9)+99"
print(TextExample)
Output:
https://www.guru99.com/python-escape-characters.html 7/10
12/13/24, 11:29 PM Python Escape Character Sequences (Examples)
Guru 99
Summary
Backslash is also regarded as a special character.
To create an escape sequence, begin with a backslash followed by the illegal
character.
Examples of escape sequences include “\b”, “\t”,”\n”,”\xhh” and “\ooo”
respectively.
“\t” allows inserting a space or tab between two words. It plays a similar role
to the space key present on the keyboard.
“\t” is used when the programmer wants to add space to a string at a precise
location.
Certain whitespaces help in putting a new line between python strings.
Line feed and carriage return, vertical tab, and form feed are types of
whitespaces.
Top
Top Python Python Lambda
Interview Python String
String Functions with
Questions and… split():
split(): List,
List, By… EXAMPLES
https://www.guru99.com/python-escape-characters.html 8/10
12/13/24, 11:29 PM Python Escape Character Sequences (Examples)
Python List
List
Comprehension,
Apend,…
About
About Us
Advertise with Us
Contact Us
Career Suggestion
SAP Career Suggestion Tool
Software Testing as a Career
Xero
RemotePC
QuickBooks
Interesting
eBook
Blog
Quiz
SAP eBook
Privacy Manager
https://www.guru99.com/python-escape-characters.html 9/10
12/13/24, 11:29 PM Python Escape Character Sequences (Examples)
https://www.guru99.com/python-escape-characters.html 10/10