Open In App

Ways to print escape characters in Python

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Escape characters are characters that are generally used to perform certain tasks and their usage in code directs the compiler to take a suitable action mapped to that character. Example :

'\n'  -->  Leaves a line
'\t'  -->  Leaves a space 

Python3




# Python code to demonstrate escape character
# string
 
ch = "I\nLove\tGeeksforgeeks"
 
print ("The string after resolving escape character is : ")
print (ch)


Output :

The string after resolving escape character is : 
I
Love    Geeksforgeeks

But in certain cases it is desired not to resolve escapes, i.e the entire unresolved string has to be printed. These are achieved by following ways.

Using repr()

This function returns a string in its printable format, i.e doesn’t resolve the escape sequences. 

Python3




# Python code to demonstrate printing
# escape characters from repr()
 
# initializing target string
ch = "I\nLove\tGeeksforgeeks"
 
print ("The string without repr() is : ")
print (ch)
 
print ("\r")
 
print ("The string after using repr() is : ")
print (repr(ch))


Output :

The string without repr() is : 
I
Love    Geeksforgeeks


The string after using repr() is : 
'I\nLove\tGeeksforgeeks'

 

Using “r/R”

Adding “r” or “R” to the target string triggers a repr() to the string internally and stops from the resolution of escape characters. 

Python3




# Python code to demonstrate printing
# escape characters from "r" or "R"
 
# initializing target string
ch = "I\nLove\tGeeksforgeeks"
 
print ("The string without r / R is : ")
print (ch)
 
print ("\r")
 
# using "r" to prevent resolution
ch1 = r"I\nLove\tGeeksforgeeks"
 
print ("The string after using r is : ")
print (ch1)
 
print ("\r")
 
# using "R" to prevent resolution
ch2 = R"I\nLove\tGeeksforgeeks"
 
print ("The string after using R is : ")
print (ch2)


Output :

The string without r/R is : 
I
Love    Geeksforgeeks


The string after using r is : 
I\nLove\tGeeksforgeeks


The string after using R is : 
I\nLove\tGeeksforgeeks

Using raw string notation:

Approach:

We can also use the raw string notation to print escape characters in Python. We just need to add the letter “r” before the opening quote of the string.
Algorithm:

  • Define a raw string variable with the required escape sequence.
  • Use the print() function to print the string.

Python3




string = "I\nLove\tGeeks\tforgeeks"
print(string)


Output

I
Love    Geeksforgeeks

Time Complexity: O(1)
Space Complexity: O(1)

 



Similar Reads

Python - Escape reserved characters in Strings List
Given List of Strings, escape reserved characters in each String. Input : test_list = ["Gf-g", "be)s(t"] Output : ['Gf\\-g', 'be\\)s\\(t'] Explanation : All reserved character elements escaped, by adding double \\. Input : test_list = ["Gf-g"] Output : ['Gf\\-g'] Explanation : All reserved character elements escaped, by adding double \\. Method #1
3 min read
html.escape() in Python
With the help of html.escape() method, we can convert the html script into a string by replacing special characters with the string with ascii characters by using html.escape() method. Syntax : html.escape(String) Return : Return a string of ascii character script from html. Example #1 : In this example we can see that by using html.escape() method
1 min read
Preventing Escape Sequence Interpretation in Python
Python Escape Sequence is a combination of characters (usually prefixed with an escape character), that has a non-literal character interpretation such that, the character's sequences which are considered as an escape sequence have a meaning other than the literal characters contained therein. Most Programming languages use a backslash \ as an esca
4 min read
Escape From \n Newline Character in Python
In this article, we are going to learn how to escape from \n newline characters in Python. Escape sequence interpretation is a common problem that arises anytime a program tries to process a string containing an escape character. Most of the time, the effects produced by the escape sequences are desirable but sometimes unwanted. This article will t
4 min read
How to Escape Quotes From String in Python
The single or double quotation is a unique character that can be used in a Python program to represent a string by encapsulating it in matching quotes. When the quotes are used inside a string contained in similar quotes it confuses the compiler and as a result, an error is thrown. This article will demonstrate how to escape quotes from a string in
2 min read
Escape Double Quotes for Json in Python
JSON (JavaScript Object Notation) is a popular data interchange format that uses human-readable text to represent data objects. When working with JSON in Python, you may encounter situations where you need to include double quotes within a string. Escaping double quotes is essential to ensure the proper representation of data in JSON. In this artic
3 min read
Python program to print k characters then skip k characters in a string
Given a String, extract K characters alternatively. Input : test_str = 'geeksgeeksisbestforgeeks', K = 4 Output : geekksisforg Explanation : Every 4th alternate range is sliced. Input : test_str = 'geeksgeeksisbest', K = 4 Output : geekksis Explanation : Every 4th alternate range is sliced. Method #1 : Using loop + slicing In this, we perform task
5 min read
Python | Ways to split a string in different ways
The most common problem we have encountered in Python is splitting a string by a delimiter, But in some cases we have to split in different ways to get the answer. In this article, we will get substrings obtained by splitting string in different ways. Examples: Input : Paras_Jain_Moengage_best Output : ['Paras', 'Paras_Jain', 'Paras_Jain_Moengage',
2 min read
Python | Ways to remove n characters from start of given string
Given a string and a number 'n', the task is to remove a string of length 'n' from the start of the string. Let's a few methods to solve the given task. Method #1: Using Naive Method C/C++ Code # Python3 code to demonstrate # how to remove 'n' characters from starting # of a string # Initialising string ini_string1 = 'garg_akshat' # Initialising nu
3 min read
Python | Ways to check string contain all same characters
Given a list of strings, write a Python program to check whether each string has all the characters same or not. Given below are a few methods to check the same. Method #1: Using Naive Method [Inefficient] C/C++ Code # Python code to demonstrate # to check whether string contains # all characters same or not # Initialising string list ini_list = [
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg