Escape Sequences in PythonIf you have been programming for some time now, you must've worked with strings. You would have printed about a lakh of strings using a print statement, and solved thousands of errors. But here is a question, "did you ever try to insert a double quote inside a string enclosed in double quotes?" Or The Answer is, "double-quote inside the string must be escaped by the eye of Python." To achieve this, we have special characters in Python called the Escape Sequences. This article discusses the concept of escape sequences, their uses, and their importance with examples. Definition: As the name suggests, an escape sequence is a sequence of characters with special meaning when used inside a string or a character. Syntax: The characters need to be preceded by a backslash character Example: \n, \t etc. The characters that we can't insert into a string are called Illegal characters, and these characters modify the string. The function of escape sequences is to insert such characters into the string without modifying the string. Let us first see a simple example: 1. Inserting single quotes into a string:#without using \' Output: string = 'Hi! 'man' ^ SyntaxError: invalid syntax Understanding: Python searches for an ending-single quote in the string 'Hi!'man'. Hence, it raises an error.
#Using '' in the stringOutput: Hi! man Understanding: Here, 'Hi!' and 'man' are considered two separate strings, so no error is raised. The number of quotes is even. But if we try to insert something between 'Hi!' and 'man', Python gets confused again and raises an error. Example: Output: string = 'Hi!'!'man' ^ SyntaxError: invalid syntax Using the escape sequence:#Using \' Output: By using \: Hi! 'man You can observe that our problem is now solved. Also, another solution in this situation is that we can use double quotes to enclose the string and use 'in the string: Output: By using \: Hi!'man There are more escape sequences in Python that helps the programmer to print different types of invalid statements:
Now, let us see the examples for the sequences to understand their uses: 1. /"Output: string = "Hi!"man" ^ SyntaxError: invalid syntax Output: By using \: Hi!"man Understanding: You can see that without using the backslash character, we got an error, and when we placed a / before the ", we got it printed. 2. \nOutput: string = "Hi!"man" Output: By using : Hi! man Understanding: In the string, "By using \n", unknowingly, we used \n. We need it to be printed, but we don't want its functionality. What to do? Use an escape sequence: Output: By using \n: Hi man 3. \tOutput: Without using \: Hi! man Output: By using \t: Hi! man Understanding: We wrote the normal statement in the first code giving normal spaces. In the second code, we used the \t to give a tabular space between Hi! and man. 4. \rOutput: Without using \r: Hi! man Output: manusing \r: Hi! Understanding: When we use \r, all the content after \r will be brought to the front by replacing all the existing characters. The string will be "By using \\r: Hi!\rman" The content after \r is "man". This word will be brought to the front of the string by replacing the existing characters-"By_" making the string man using \r: Hi!
Output: man Here, "man" succeeds \r. it will be brought to the front of the string, replacing Hi!; All the three characters in Hi! will be replaced by man making the output "man". 5. \bOutput: Without using \b: Hi! man Output: By using \b: Himan Understanding: \b is the backspace character. It deletes the preceding character like the backspace key in our keyboards and calculators. We gave \b between Hi! and man. The preceding character is "!"; hence, it is deleted. 6. \oooooo represents octal numbers. We can get the characters equivalent to octal representations by using this character. Example: Output: ??89 Understanding: \543 represents ? \432 represents ? \789 represents 89 7. \xhhhh represents octal numbers. We can get the characters equivalent to hexadecimal representations by using this character. Example: Output: T4 Understanding: \x54 represents T \x34 represents 4 The sequences discussed in the article are the most commonly used escape characters. These are not the only escape sequences in Python. More sequences will be useful in different scenarios while writing different logic. Next TopicPYTHON NULL STATEMENT |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India