Posted in

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

Python Escape Sequence Part 1
Python Escape Sequence Part 1
Syntax: \n
Single backslash (\)Double backslashes (\\)Single quotes (\’)Double quotes (\”)New line (\n)
Tab (\t)Backspace (\b)Carriage return (\r)Octal value (\ooo)Hexadecimal Values (\xhh)
Code:
text = 'Hello \Python'
print(text)

Output:
SyntaxWarning: invalid escape sequence '\P'
Code:
text = 'Hello \ Python'
print(text)

Output:
SyntaxWarning: invalid escape sequence '\ '
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.
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.
Code:
text = 'Come here\go there'
print(text)

Output:
SyntaxWarning: invalid escape sequence '\g'
Code:
text = 'Come here\\go there'
print(text)

Output:
Come here\go there
Code:
text = 'Come here\\go there, again come here\\go there'
print(text)

Output:
Come here\go there, again come here\go there
Code:
text = 'Come here\\\\go there'
print(text)

Output:
Come here\\go there
Code:
text = 'It's Raining'
print(text)

Output:
SyntaxError: unterminated string literal 
Code:
text = "It's Raining"
print(text)

Output:
It's Raining
Code:
text = 'It\'s Raining'
print(text)

Output:
It's Raining
Code:
text = 'It\'s Raining, It\'s continue raining'
print(text)

Output:
It's Raining, It's continue raining

Click to learn for Python quotes tutorial

Code:
text = "He is "Mr. Nobody" in this house."
print(text) 

Output:
SyntaxError: invalid syntax
Code:
text = "He is \"Mr. Nobody\" in this house."
print(text)

Output:
He is "Mr. Nobody" in this house.
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.
Code:
print('FirstLine \n')
print('SecondLine')

Output:
FirstLine 

SecondLine
Code:
print('FirstLine \n\n')
print('SecondLine')

Output:
FirstLine 


SecondLine

Click for the Python Escape Sequence (Part 2) Tutorial

3 thoughts on “Python Escape Sequence (Part 1): What Are The Escape Characters In Python?

Leave a Reply

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