
Escape Sequences in Python
Escape sequences in Python are a set of characters representing either a special character or a specific command within a text string.
What are escape sequences used for? They are employed to insert characters that can't be directly typed, such as newline or tabulation. For example, the Ascii control characters not associated with a graphical symbol (e.g., \n, \t), certain printable characters (e.g., \', \" , \\), Unicode character sequences, etc.
In Python, an escape sequence always begins with a backslash (`\`) followed by one or more characters.
For instance, the newline command is written as \n
\n
You can add this sequence to any text string
myVar = "Hello\nWorld"
When you print the Python string, the print() function interprets the \n escape sequence and executes its associated command (newline), inserting a line break between the two words.
print(myVar)
Hello
World
Remember, despite taking up two or more positions, an escape sequence is counted as a single character.
For example, define a string with two characters and two escape sequences
myVar = "Hi\n\n"
At first glance, this string seems to consist of 6 characters.
In reality, if you use the len() function, it counts only 4 because each \n escape sequence is counted as a single character.
print(len(myVar))
4
This is an important aspect to keep in mind when processing strings in Python.
Other Escape Sequences
Of course, \n is not the only escape sequence.
Here are some of the more frequently used escape sequences in programming.
Escape Sequence | Description |
---|---|
\\ | Backslash |
\' | Single quote |
\" | Double quote |
\a | PC beep |
\b | Delete last character |
\f | New page |
\n | New line |
\r | Carriage return |
\t | Horizontal tabulation (TAB) |
\v | Vertical tabulation |
When to Use Escape Sequences?
Escape sequences have various practical applications.
They are useful for printing strings across multiple lines, inserting precise tabulations or spacings, managing file paths in Windows systems, inserting non-printable special characters, and more.
For instance, if you want to insert a single quote inside a string, you might run into trouble if the string is enclosed in single quotes, as the presence of an intermediate single quote would terminate the string.
To circumvent this issue, you can insert the internal single quote as an escape sequence \'
print('This is the\'art')
This is the art
A common problem is inserting a backslash in a string, as Python interprets it as the beginning of an escape sequence.
To insert a backslash as a character, you can use the escape sequence \\.
print("C:\\Programs\\Python")
In this case, Python treats the second backslash of the \\ sequence as a character rather than a command.
C:\Programs\Python
Alternatively, if you don't want Python to interpret backslashes as the beginning of an escape sequence, you can use a raw string by prefixing `r` or `R` to the string itself.
print(r"C:\Users\Document")
C:\Users\Document
You can also use escape sequences to insert any character using its hexadecimal Unicode code
For example, to insert a heart symbol into a string, you can type \u2764
print("This is a heart \u2764")
The string is displayed in this way
This is a heart ❤
Remember, effective use of escape sequences can make your code more readable and manageable, especially when working with complex texts or file paths.
Escape sequences do not work in byte strings. In byte strings, control sequences are not interpreted or executed by the Python language. Thus, you can only insert them in text strings.
print(b"Hi\n\n")
Hi\n\n