Python Ignoring escape sequences in the string

Here, we will figure out how to overlook escape arrangement in python programming language and print the genuine appointed worth?

First observe, how escape succession works?

In the beneath example, we are utilizing a portion of the escape succession and their outputs, we are printing single statement (\’), twofold statements (\”), printing way (twofold slice) (\) and utilizing hexadecimal qualities (\x).

Program:

#printing single quote
str1 = "Hi, I\'m JustTechReview"	
#printing double quotes
str2 = "\"Hello world\""
#printing path
str3 = "D:\working_dir\python_program"
#using hexadecimal values
str4 = "This is  \x49\x6E\x63\x6C\x75\x64\x65\x48\x65\x6C\x70"

print(str1);
print(str2);
print(str3);
print(str4);

Output:

Hi, I'm JustTechReview
"Hello world"
D:\working_dir\python_program
This is  JustTechReview

Disregarding Escape Sequences:

To disregarding escape arrangements in the string, we make the string as “crude string” by putting “r” before the string. “crude string” prints as it allocated to the string.

Program:

#ignoring escape sequences

#ignoring single quote escape sequences
str1 = r"Hi, I\'m JustTechReview"	
#ignoring double quotes escape sequences
str2 = r"\"Hello world\""
#ignoring path escape sequences
str3 = r"D:\working_dir\python_program"
#ignoring hexadecimal values escape sequences
str4 = r"This is  \x49\x6E\x63\x6C\x75\x64\x65\x48\x65\x6C\x70"

print(str1);
print(str2);
print(str3);
print(str4);

Output:

Hi, I\'m JustTechReview
\"Hello world\" 
D:\working_dir\python_program
This is  \x49\x6E\x63\x6C\x75\x64\x65\x48\x65\x6C\x70

Leave a Comment

error: Alert: Content is protected!!