Home
Aptitude
Categories


Home » Python » Python programs

Python | Ignoring escape sequences in the string



Here, we are going to learn how to ignore escape sequence in python programming language and print the actual assigned value?
Submitted by IncludeHelp, on November 28, 2018

First see, how escape sequence works?

In the below example, we are using some of the escape sequence and their outputs, we are printing single quote (\'), double quotes (\"), printing path (double slash) (\\) and using hexadecimal values (\x).

Program:

#printing single quote
str1 = "Hi, I\'m IncludeHelp"	
#printing double quotes
str2 = "\"Hello world\""
#printing path
str3 = "D:\\work_folder\\python_works"
#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 IncludeHelp
"Hello world"
D:\work_folder\python_works 
This is  IncludeHelp 

Ignoring Escape Sequences

To ignoring escape sequences in the string, we make the string as "raw string" by placing "r" before the string. "raw string" prints as it assigned to the string.

Program:

#ignoring escape sequences

#ignoring single quote escape sequences
str1 = r"Hi, I\'m IncludeHelp"	
#ignoring double quotes escape sequences
str2 = r"\"Hello world\""
#ignoring path escape sequences
str3 = r"D:\\work_folder\\python_works"
#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 IncludeHelp  
\"Hello world\" 
D:\\work_folder\\python_works  
This is  \x49\x6E\x63\x6C\x75\x64\x65\x48\x65\x6C\x70

Other python string programs...






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.