How to Include Emojis in Your Python Code

Delta Online News

An emoji is a small digital image used to express an idea or emotion. Integrating emojis with programming can be fun. It makes programming an enjoyable task. You can use emojis in comments, commit messages or directly in code. You can convert boring texts like production logs and documentation into interesting text by using emojis. Even people tend to pick lines having emojis which increases productivity.

Since Python is known for its versatility, you can perform many operations on emoji using Python.

Printing emojis using Python seems to be difficult but it’s deceptively simple. You can use Unicode characters, CLDR names or Python library emoji to print emojis.

Using Unicode Characters to Print Emoji

Unicode is a universal character encoding standard that assigns a code to every character and symbol in every language in the world. Every emoji has a unique Unicode assigned to it. When using Unicode with Python, replace “+” with “000” from the Unicode. And then prefix the Unicode with “\”.

For example- U+1F605 will be used as \U0001F605. Here, “+” is replaced with “000” and “\” is prefixed with the Unicode.

# grinning face
print("\U0001F600")
# beaming face with smiling eyes
print("\U0001F601")
# grinning face with sweat
print("\U0001F605")
# rolling on the floor laughing
print("\U0001F923")
# face with tears of joy
print("\U0001F602")
# slightly smiling face
print("\U0001F642")
# smiling face with halo
print("\U0001F607")
# smiling face with heart-eyes
print("\U0001F60D")
# zipper-mouth face
print("\U0001F910")
# unamused face
print("\U0001F612")

The above code will give the following output:

?
?
?
?
?
?
?
?
?
?

Using CLDR Short Names to Print Emoji

CLDR collects short character names and keywords for Emoji characters and sequences. This method is more comfortable and easy to use.

# smiling face with sunglasses
print("\N{smiling face with sunglasses}")
# grinning face
print("\N{grinning face}")
# loudly crying face
print("\N{loudly crying face}")
# rolling on the floor laughing
print("\N{rolling on the floor laughing}")
# face with tears of joy
print("\N{face with tears of joy}")
# slightly smiling face
print("\N{slightly smiling face}")
# smiling face with halo
print("\N{smiling face with halo}")
# angry face
print("\N{angry face}")
# zipper-mouth face
print("\N{zipper-mouth face}")
# unamused face
print("\N{unamused face}")

The above code will give the following output:

?
?
?
?
?
?
?
?
?
?

Using the Emoji Library to Print Emoji

This library makes it easy to integrate emojis with Python programs. But you need to install this library before using it. Make sure you have pip installed on your system. Run the following in the command prompt:

pip install emoji

This will install the emoji Python library. Note that to use this library in your Python program, you will have to import the library.

# Import required libraries
from emoji import emojize
# smiling face with sunglasses
print(emojize(":smiling_face_with_sunglasses:"))
# grinning face
print(emojize(":grinning_face:"))
# loudly crying face
print(emojize(":loudly_crying_face:"))
# rolling on the floor laughing
print(emojize(":rolling_on_the_floor_laughing:"))
# face with tears of joy
print(emojize(":face_with_tears_of_joy:"))
# slightly smiling face
print(emojize(":slightly_smiling_face:"))
# smiling face with halo
print(emojize(":smiling_face_with_halo:"))
# angry face
print(emojize(":angry_face:"))
# zipper-mouth face
print(emojize(":zipper-mouth_face:"))
# unamused face
print(emojize(":unamused_face:"))

The above code will give the following output:

?
?
?
?
?
?
?
?
?
?

Related: How to Get New Emojis on Android

Extracting All Emojis From the Text

You can easily extract all the emojis from the text using Python. It can be done using regular expression. Run the following command in the command prompt to install the regex library:

pip install regex

re.findall() method is used to find all the emojis from the text.

# Import required libraries
import regex as re
# Text from which you want to extract emojis
text = 'We ? want ? to ? extract ? these ? emojis '
# Using regular expression to find and extract all emojis from the text
emojis = re.findall(r'[^\w\⁠s,. ]', text)
print(emojis)

The following output will be displayed:

['?', '?', '?', '?', '?']

Converting Emoji Into Text

You can convert emoji into text using Python’s demoji library. To install the demoji library, run the following command:

pip install demoji

After you have installed the demoji library, you’ll have to download data from the Unicode Consortium’s emoji code repository as the emoji list itself is frequently updated and changed. Paste the following code in a Python file and then run it to download the required data.

# Importing demoji library
import demoji
demoji.download_codes()

Finally, use the following code to convert emojis into text.

# Import required libraries
import demoji
# Text from where you want to convert emojis
text = "Convert ? the ? given emojis ? to ? text"
emojis = demoji.findall(text)
# Print converted emojis
print(emojis)

Output:

{'?': 'unamused face',
'?': 'grinning face with smiling eyes,
'?': 'angry face',
'?': 'smiling face with sunglasses,
}

Replace Emoji With Its Meaning

If you want to replace emojis with their meaning, you can easily do it using the emoji library. Make sure to install the emoji library using pip before executing the following code.

# Import required libraries
import emoji
# Text from where you want to replace emojis
text = """These are some of the most used emojis
1. ?
2. ?
3. ?"""
replaced_text = emoji.demojize(text, delimiters=("", ""))
# Printing replaced text
print(replaced_text)

The above code will give the following output:

These are some of the most used emojis
1. face_with_tears_of_joy
2. smiling_face_with_heart-eyes
3. rolling_on_the_floor_laughing

Removing Emoji From the Text in Python

You can remove all emojis from the text with the help of regular expressions in Python.

# Importing Regular Expression Library
import re
# Text from where you want to remove all emojis
text = """These are some of the most used emojis
1. ? Emoji 1
2. ? Emoji 2
"""
# Printing the text with emojis
print(text)
# Function to remove emoji from text
def removeEmoji(text):
regrex_pattern = re.compile(pattern = "["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
"]+", flags = re.UNICODE)
return regrex_pattern.sub(r'',text)
# Printing the text without emojis
print(removeEmoji(text))

The above code will give the following output:

These are some of the most used emojis
1. ? Emoji 1
2. ? Emoji 2
These are some of the most used emojis
1. Emoji 1
2. Emoji 2

Make Programming Fun With Emojis

Emojis are now considered an integral part of text communication. Using the power of Python you can perform many operations on them. Get a habit of using emojis in comments, commit messages, etc. to make programming fun.

Both Emoticon and Emoji are now being used extensively in various organisations. You can even make your own emoji to express yourself over text.

MUO – Feed