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?
By IncludeHelp Last updated : February 13, 2024

How escape sequence works in Python?

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).

Example

#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 

Ignore escape sequences in the string

To ignore 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.

Example

#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

Python String Programs »

To understand the above programs, you should have the basic knowledge of the following Python topics:

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.