Methods for printing escape characters in Python

Python Methods and Functions

Example:

 '' - & gt; Leaves a line '' - & gt; Leaves a space 

# Python code for showing the escape character
# string

 

ch = " I Love Geeksforgeeks "

  

print ( "The string after resolving escape character is:" )

print (ch)

Escape:

 The string after resolving escape character is: I Love Geeksforgeeks 

But in some cases it is desirable not to allow exits, i.e. the entire unresolved line has
being printed. This is accomplished in the following ways.

Using repr ()

This function returns a string in printable format, i.e. does not allow escape sequences.

# Python code for printing demonstration
# escape characters from repr ()

 
# target string initialization

ch = "I Love Geeksforgeeks"

 

print ( "The string without repr () is:" )

print (ch)

 

print ( "" )

  

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 Love Geeksforgeeks' 

Using" R / R "

Adding" r "or" R "to the target line starts repr () for the inner line and stops with escape characters enabled.

# Python code for printing demonstration
# escape characters from & quot; r & quot; or & quot; R & quot;

 
# target string initialization

ch = "I Love Geeksforgeeks"

 

print ( "The string without r / R is:" )

print (ch)

  

print ( "" )

 
# using & quot; r & quot; to prevent permission

ch1 = r "I Love Geeksforgeeks"

 

print ( "The string after using r is:" )

print (ch1)

 

print ( " " )

  
# using "R" to prevent permission

ch2 = R "I Love Geeksforgeeks"

 

pri nt ( "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 Love Geeksforgeeks The string after using R is: I Love Geeksforgeeks 




Tutorials