Print escape characters in Python
In this tutorial, we are going to learn how to print escape characters in Python.
Mostly in Python, we use two escape characters.
- ‘\n’ -> Leaves a line
- ‘\t’ -> Leaves a space
How to print escape character using r/R in Python
We can print the escape character useing r or R before the string.
Let us understand it with the help of an example
Example to print escape character using r.
#without using r
inpt1 = "hii,\ni am\tprogrammer "
print("output of inpt1:-")
print (inpt1)
#using r
inpt2 = r"hii,\n I am \t programmer "
print("output of inpt2:-")
print (inpt2))OUTPUT:-
output of inpt1:- hii, i am programmer output of inpt2:- hii,\n I am \t programmer
Example to print escape characters using R in Python
#without using R
inpt1 = "hii,\ni am\tprogrammer "
print("output of inpt1:-")
print (inpt1)
#using R
inpt2 = R"hii,\n I am \t programmer "
print("output of inpt2:-")
print (inpt2)OUTPUT:-
output of inpt1:- hii, i am programmer output of inpt2:- hii,\n I am \t programmer
Example 2:-
#without using r
inpt1 = "hii,\n this line\twill not print escape character "
print("output of inpt1:-")
print (inpt1)
#using r
inpt2 = r"hii,this are escape character \n and \t "
print("output of inpt2:-")
print (inpt2)output of inpt1:- hii, this line will not print escape character output of inpt2:- hii,this are escape character \n and \t
Also read:
Leave a Reply