What are Escape Characters ?
Escape characters are used for good programming in Python and to keep large programs clean. Escape characters are used when we need to write such symbols in a string which are normally interpreted differently by Python. Escape characters in Python start with a backslash (\). Some important escape characters used in Python language are given below.
1. \n : Newline Character : \n (Backslash n) is used to break a line in the middle of a string. As soon as the compiler reads \n, it moves all the text that follows it to the next line (New Line). It is used to organize the output as a paragraph or list.
print("Hello\nWorld")
Hello World
print("Hello\nWorld") = > This Print() shows Hello in the first line and World in the second line on the screen.
2. \t : Horizontal Tab : \t (Backslash t) is used to give a large space (which is equal to 4 or 8 normal spaces on keyboard) between two words or values ββwithin a string. Its biggest advantage is when we need to arrange the data in table format or in the form of columns at equal distance on the screen.
print("Hello\tWorld")
Hello World
This print() function displays the words "Hello" and "World" on the screen by giving a space of 1 tab between them.
3. \' : Single Quote : If you have enclosed your entire string in single quotes ('...'), and you want to include another single quote within that string (e.g. Aman's book in English), Python gets confused about the end of the string and throws a SyntaxError. To avoid this error, \ is placed before the inner single quote.
print('It\'s Python')
It's Python
print('It\'s Python') This function displays single quotes(') between words.
4. \" : Double Quote : Similar to the rule for \', if your main string is enclosed within double quotes ("..."), and you want to display double quotes inside to highlight a dialogue or a specific word, you need to use \". This tells the compiler that the double quote is not meant to terminate the string, but is just normal text to be printed on the screen.
print("He said \"Hi\"")
He said "Hi"
print("He said \"Hi\"") This function displays double quotes(") between words.
5. \b : Backspace : \b (Backslash b) works like the 'Backspace' button on the keyboard. Wherever \b appears in a string, it removes the preceding (leftmost) character or space from the output screen. This is often used for dynamic formatting or to quickly remove unwanted spaces.
print("Hello\bWorld")
HellWorld
print("Hello\bWorld") This function shows the entire words by removing the last letter of the first word of both the words.
Features Of Escape Characters
1. Syntax Non-Disruption : They allow developers to include legally restricted
characters (like internal double quotes " inside a double-quoted string) without prematurely
breaking the string boundaries or causing a SyntaxError.
2. ASCII-Level Translation : They act as non-printable ASCII instructions. When
the Python interpreter parses them, it translates the two character symbols (like \ and n) into
a single byte payload instruction (like the ASCII LF/Line Feed byte 0x0A).
3. Cross-Platform Output Normalization : Characters like \n abstract away
lower-level OS differences. Python intelligently handles whether the underlying host platform
expects a Carriage Return and Line Feed (\r\n on Windows) or just a Line Feed (\n on Unix/Linux
systems).
4. Universal Type Integrity : Escape sequences are fully embedded inside
standard str primitives. They do not alter the data type of the string; they only alter its
visual and structural layout upon evaluation or printing.