13.1 List of Escape Characters
Escape characters came about in the early days of computing as a way to insert control codes into text strings. Historically, they were used to manage devices like printers and teletypes, allowing commands such as new line or carriage return to be inserted directly into data streams.
With the development of programming, these characters became part of standard programming languages, including C, from where they migrated to Python and other languages. In Python, escape characters are denoted by a backslash \ followed by a character that defines their function, like \n for a new line.
Escape characters in Python play a crucial role in string handling, allowing special characters that would otherwise be hard or impossible to directly include in strings.
Here's a table with the most popular ones
| Escape Character | Description |
|---|---|
\\ |
Backslash. |
\' |
Single quote. |
\" |
Double quote. |
\n |
New line. |
\r |
Carriage return. |
\t |
Horizontal tab. |
\b |
Backspace, deletes the previous character. |
\f |
Form feed. |
\a |
Beeper. |
Form feed and beeper are leftovers from the days when text was printed or sent to a teletype. But hey, you can't change the past.
On the other hand, horizontal tab is frequently used for code alignment.
13.2 Inserting Unicode Characters
At the dawn of computing, computers could only handle strings containing Latin letters, digits, numbers, and a few symbols. The first character set (ASCII encoding) only included 128 characters, including control ones like "form feed" :)
The idea of Unicode emerged only in 1987 when Joe Becker from Xerox and Lee Collins from Apple began discussing the possibility of creating a new character encoding system that could support all the world's writing systems.
They envisioned Unicode as a 16-bit system, allowing for 65,536 unique characters, which they thought would be enough to cover all known scripts. But of course, it wasn't :)
Over time, Unicode expanded to include not only more "language scripts" but also an extensive set of symbols, emojis, and historical texts. Unicode 13.0, released in March 2020, contains over 143,000 characters, covering more than 150 "scripts" and numerous symbol sets, making it a comprehensive encoding system for the digital age.
Unicode was added to Python in version 2.0, released in October 2000. This significant change allowed Python to better support international languages and characters, simplifying international application development.
Python code file encoding supports Unicode, so you can insert characters from any language into text:
print("Come to the Dark Side") # English
print("Ven al Lado Oscuro") # Spanish
print("Komm auf die dunkle Seite") # German
print("暗黒面に来なさい") # Japanese
print("来到黑暗面") # Chinese
You can insert a Unicode character by its number using hexadecimal encoding:
| Escape Character | Description |
|---|---|
| \\uXXXX | Unicode character with 16-bit hexadecimal value XXXX. |
| \\UXXXXXXXX | Unicode character with 32-bit hexadecimal value XXXXXXXX. |
13.3 Inserting Emojis
Inserting emojis into text and Python code can be a fun way to enhance the visual appeal and emotional tone of messages. Emojis are Unicode symbols and can be inserted directly into Python's string data.
Emojis in Python use the standard Unicode, which includes thousands of characters, including many emojis. Each emoji has a unique Unicode number, allowing it to be inserted into text.
Examples of inserting emojis
To insert an emoji, just copy it from the internet and paste it into the text
# Example of emoji output
print(" 😀 ") # will output 😀
Examples of inserting emojis using Unicode
To insert an emoji, just know its code and use it in a string with the u prefix:
# Example of emoji output
print(u"\U0001F600") # 😀
Working with Libraries
There are libraries, like emoji, that make working with emojis easier:
# Using the emoji library
from emoji import emojize
print(emojize(":grinning_face:"))
Emojis are widely used in mobile and web applications, and Python is no exception. They can add clarity and emotional expressiveness to chatbots, forums, comments, and other types of user interaction.
GO TO FULL VERSION