Posted in

Python Escape Sequence (Part 2): What Are The Escape Characters In Python?

Python Escape Sequence Part 2
Python Escape Sequence Part 2

Click for Python Escape Sequence (Part 1) Tutorial

Python Escape Sequence

Types of Escape Characters in Python

Tab (\t)

Code:
text1 = 'Hello Python'
text2 = 'Hello \tPython'
print(text1)
print(text2)

Output:
Hello Python
Hello 	Python
Code:
text1 = 'Hello Python'
text2 = 'Hello \t\tPython'
print(text1)
print(text2)

Output:
Hello Python
Hello 		Python
Code:
text1 = 'Hello \tPython \tprogramming \tlanguage'
print(text1)

Output:
Hello 	Python 	programming 	language
Code:
text1 = 'Hello   Python'
text2 = 'Hello \tPython'
print(text1)
print(text2)

Output:
Hello   Python
Hello 	Python

Backspace (\b)

Code:
text1 = 'Hello Python'
text2 = 'Hello \bPython'
print(text1)
print(text2)

Output:
Hello Python
HelloPython
Code:
text1 = 'Hello    \bPython'
print(text1)

Output:
Hello   Python
Code:
text1 = 'Hello    \b\bPython'
print(text1)

Output:
Hello  Python
Code:
text1 = 'Hello \bPython'
print(text1)
text2 = 'Hello \b\bPython'
print(text2)

Output:
HelloPython
HellPython
Code:
text2 = 'Hello \b\b\bPython'
print(text2)

Output:
HelPython
Code:
text = '\bHello'
print(text)

Output:
Hello
Code:
text = 'Hello \t\bPython'
print(text)

Output:
Hello Python
Code:
text = 'Hello \b\tPython'
text2 = 'Hello   Python'
print(text)
print(text2)

Output:
Hello	Python
Hello   Python

Carriage return (\r)

Code:
text = 'Hello \rPython'
print(text)

Output:
Python
Code:
text = 'Hello \rPython \rprogramming \rlangauge'
print(text)

Output:
langauge

Octal value (\ooo)

Code:
text = 'Life is \141 life'
print(text)

Output:
Life is a life
Code:
text = '\141 \142 \143'
print(text)

Output:
a b c

Hexadecimal Values (\xhh)

Code:
text = '\x44'
print(text)

Output:
D
Code:
text = '\x34 \x44 \x52 \x23'
print(text)

Output:
4 D R #

Rules for Escape Sequence in Python

Escape sequence works only in strings

Code:
text1 = ['Python \nProgramming']
text2 = 'Python \nProgramming'
print(text1)
print(text2)

Output:
['Python \nProgramming']
Python 
Programming
Code:
text1 = ('apple', '\nPython')
text2 = 'apple \nPython'
print(text1)
print(text2)

Output:
('apple', '\nPython')
apple 
Python

No space between backslash and escape character

Code:
text1 = 'apple \nPython'
Code:
text1 = 'apple \ nPython'
print(text1)

Output:
SyntaxWarning: invalid escape sequence '\ '

Example 1:

Code:
text1 = 'Apple \nPython \nJava \nProgramming'
print(text1)

Output:
Apple 
Python 
Java 
Programming

Example 2:

Code:
text1 = 'Apple \n Python \n Java \n Programming'
print(text1)

Output:
Apple 
 Python 
 Java 
 Programming

Example 3:

Code:
text1 = 'Apple \n    Python \n  Java \n     Programming'
print(text1)

Output:
Apple 
    Python 
  Java 
     Programming

Escape sequence won’t work in raw strings

Code:
text1 = 'Normal string'
text2 = r'Raw string'
print(type(text1), type(text2))

Output:
<class 'str'> <class 'str'>
Code:
text1 = 'Normal \nString'
text2 = r'Raw \nstring'
print(text1)
print(text2)

Output:
Normal 
String
Raw \nstring

Conclusion

Click for Python Data Type (Part 1)

One thought on “Python Escape Sequence (Part 2): What Are The Escape Characters In Python?

Leave a Reply

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