Python Emoji Module with Examples

People nowadays communicate their emotions with emojis rather than extended lengths of text. Emojis have become an important partĀ of our everyday communication with one another.

Emoji Module:

In Python, we may print the Emojis in a number of ways. They are:

printing Emojis with Unicodes, CLDR names, and the emoji module.

UsingUnicodes:

Every emoji is assigned a Unicode code.

Replace “+” with “000” in the list of Unicodes. For example, “U+1F600” will become “U0001F600,” with the Unicode prefixed by “\” and printed.

Emojis have a CLDR short name that can be utilized as well.

# Print the grinning face emoji
print("\U0001f600")
 
#  grinning squinting face emoji
print("\U0001F606")
print("\U0001F649")
# Print the rolling on the floor laughing emoji
print("\U0001F923")

Output:

šŸ˜€
šŸ˜†
šŸ™‰
šŸ¤£

Using CLDR short name:

# Print the grinning face emoji
print("\N{grinning face}")
 
# Print the slightly smiling face emoji
print("\N{slightly smiling face}")
 
# Print the winking face emoji
print("\N{winking face}")

Output:

šŸ˜€
šŸ™‚
šŸ˜‰

Using emoji Module:

Before starting, install the emoji Module as shown below:

pip install emoji

Output:

Collecting emoji Downloading emoji-1.6.1.tar.gz (170 kB) 
|ā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆ| 170 kB 15.6 MB/s Building wheels for 
collected packages: emoji Building wheel for emoji (setup.py) ... done 
Created wheel for emoji: filename=emoji-1.6.1-py3-none-any.whl size=169314 
sha256=5e835eb87674dd1171fd982e5e4c9490dce2d715db0e401221ff8e8902e4b4c1 Stored 
in directory: /root/.cache/pip/wheels/ea/5f/d3/03d313ddb3c2a1a427bb4690f1621eea
60fe6f2a30cc95940f Successfully built emoji Installing collected packages: emoji
Successfully installed emoji-1.6.1

emojize() Method:

The CLDR short name must be passed as a parameter to the emojize() method. The relevant emoji is then returned. In the CLDR short name, replace the spaces with underscores.

The emojize() method takes as a parameter the name of the emoji enclosed in a colon (:).

Example:

Approach:

  • Import emoji module using the import keyword.
  • Pass some random emoji name to the emojize() function and print it.
  • Similarly, print the other emojis.
  • The Exit of the Program.

Below is the implementation:

# Import emoji module using the import keyword.
import emoji
# Pass some random emoji name to the emojize() function and print it.
print(emoji.emojize(":thinking_face:"))
# Similarly print the other emoji's
print(emoji.emojize(":zany_face:"))
print(emoji.emojize(":zipper-mouth_face:"))

Output:

šŸ¤”
šŸ¤Ŗ 
šŸ¤

demojize() function:

It is a function that converts the emoji given into its CLDR short name.

Approach:

Below is the implementation:

# Import emoji module using the import keyword.
import emoji
# Pass some random emoji to the demojize() function to get the CLDR short name 
# of the emoji given and print it.
print(emoji.demojize("šŸ¤”"))
# Similarly print the other emoji's CLDR short name 
print(emoji.demojize("šŸ¤Ŗ"))
print(emoji.demojize("šŸ¤"))

Output:

:thinking_face: 
:zany_face: 
:zipper-mouth_face: