Introduction

One important aspect of Python programming is string manipulation. In Python, as in any programming language, strings are an essential data type which is used to store textual data. Sometimes, when working with strings, we need to include special characters like new lines and tabs which are not easily represented in regular strings. In such cases, we can escape sequence the string to add these special characters. This tutorial will cover escape sequencing a string in Python, helping you enhance your string manipulation skills and improve your programming capabilities.

Table of Contents :

  • What is escape sequencing
  • Inserting characters using escape sequencing
    • Inserting a Newline
    • Inserting a Tab
    • Inserting a Backslash
    • Inserting a Single Quote
    • Inserting a Double Quote
    • Inserting Unicode Characters

What is escape sequencing :

  • Escape sequencing is a technique used in string literals to insert characters that are illegal or difficult to include in a string directly. 
  • This is achieved by using an escape character followed by a special character or code. 
  • The escape character is usually the backslash  symbol. 
  • Escape sequences enable you to insert characters like 
    • newline, 
    • tab, 
    • backslash, 
    • single quotes
    • double quotes, 
    • Unicode characters. 
  • By placing the escape character before a special or reserved character, Python interprets it as a literal representation of the character itself, rather than its special meaning in the string. 
  • Escape sequencing is an important feature of string handling in Python and many other programming languages.

Inserting characters using escape sequencing :

  • Escape sequences enable you to insert characters like 
    • newline, 
    • tab, 
    • backslash, 
    • single quotes
    • double quotes, 
    • Unicode characters. 

Inserting a Newline :

  • We can use the escape sequence  \n  to insert a newline character in the string.
  • Code Sample : 

my_string = "Hello\nworld!"
print(my_string)          
 

# Output  
# Hello
# world!


Inserting a Tab :

  • We can use the escape sequence  \t  to insert a tab character in the string.
  • Code Sample : 

my_string = "Hello\tworld!"
print(my_string)          


# Output  
# Hello   world!
 
 

Inserting a Backslash :

  • We can use the escape sequence  \\  to insert a backslash character in the string.
  • Code Sample : 

my_string = "C:\\Users\\John\\Documents"
print(my_string)          

# Output  
# C:\Users\John\Documents


Inserting a Single Quote :

  • We can use the escape sequence  \'  to insert a single quote in a string enclosed in single quotes.
  • Code Sample : 

my_string = 'It\'s raining outside.'
print(my_string)          

# Output  
# It's raining outside.

 
 

Inserting a Double Quote :

  • We can use the escape sequence  \"  to insert a double quote in a string enclosed in double quotes.
  • Code Sample : 

my_string = "She said, \"I'll be back soon.\""
print(my_string)         
 
# Output  
# She said, "I'll be back soon."

Inserting Unicode Characters :

  • We can use the escape sequence  \uXXXX  to insert a Unicode character into the string, where `XXXX` represents the Unicode code point in hexadecimal.
  • Code Sample : 

my_string = "Hello \u2764"
print(my_string)          

# Output  
# Hello


Prev. Tutorial : Modify or delete a string

Next Tutorial : Formatting strings