How to Check if a String is an Emoji in Python?

In this tutorial, I will explain how to check if a string is an emoji in Python. I recently faced an issue where I needed to filter out emojis from user input in a Python-based web application targeting users in the USA. This led me to explore various methods to identify and handle emojis in Python. I will share my findings and provide detailed examples to help you implement emoji detection in your projects.

Methods to Check if a String is an Emoji in Python

Several methods exist in Python to check if a string contains an emoji. We will explore the various approaches:

1. Use Unicode Ranges

Emojis are represented by specific Unicode points. By checking if a character falls within these ranges, we can determine if it is an emoji.

Here’s a Python function to check if a string is an emoji using Unicode ranges:

def is_emoji(character):
    # Get the Unicode code point of the character
    code_point = ord(character)
    # Check if the code point is in one of the emoji ranges
    return (
        code_point in range(0x1F600, 0x1F64F) or
        code_point in range(0x1F300, 0x1F5FF) or
        code_point in range(0x1F680, 0x1F6FF) or
        code_point in range(0x1F700, 0x1F77F)
    )

# Example usage
string = "😊"
if len(string) == 1 and is_emoji(string):  # Ensure only one character is checked
    print("The string is an emoji")
else:
    print("The string is not an emoji")

Output:

The string is an emoji

I have executed the above example code and added the screenshot below.

String is an Emoji in Python

This function checks if the character falls within the common emoji Unicode ranges. For instance, the smiley face emoji 😊 falls within the range 0x1F600 to 0x1F64F.

Read How to Check if a Variable Exists in Python?

2. Use the emoji Library

The emoji library provides a convenient way to work with emojis in Python. It includes functions to check if a string is an emoji and to extract emojis from text.

First, install the emoji library using pip:

pip install emoji

Then, use the library to check if a string is an emoji:

import emoji

def is_emoji(string):
    return string in emoji.UNICODE_EMOJI['en']

# Example usage
string = "🇺🇸"
if is_emoji(string):
    print("The string is an emoji")
else:
    print("The string is not an emoji")

Output:

The string is an emoji

I have executed the above example code and added the screenshot below.

Check if a String is an Emoji in Python

The string "🇺🇸" is a flag emoji representing the United States.The emoji.is_emoji() function correctly identifies it as an emoji because it checks if the input string is a valid emoji character or sequence.

Read How to Check if a Variable is a Number in Python?

3. Use Regular Expressions

Regular expressions (regex) provide a useful way to search for patterns in text. We can use regex to detect emojis in a string.

Here’s a Python function to check if a string contains an emoji using regex:

import re

emoji_pattern = re.compile(
    "[" 
    u"\U0001F600-\U0001F64F"  # emoticons
    u"\U0001F300-\U0001F5FF"  # symbols & pictographs
    u"\U0001F680-\U0001F6FF"  # transport & map symbols
    u"\U0001F700-\U0001F77F"  # alchemical symbols
    u"\U0001F780-\U0001F7FF"  # Geometric Shapes Extended
    u"\U0001F800-\U0001F8FF"  # Supplemental Arrows-C
    u"\U0001F900-\U0001F9FF"  # Supplemental Symbols and Pictographs
    u"\U0001FA00-\U0001FA6F"  # Chess Symbols
    u"\U0001FA70-\U0001FAFF"  # Symbols and Pictographs Extended-A
    "]+", flags=re.UNICODE)

def contains_emoji(string):
    return bool(emoji_pattern.search(string))

# Example usage
string = "Hello 🇺🇸!"
if contains_emoji(string):
    print("The string contains an emoji")
else:
    print("The string does not contain an emoji")

Output:

The string does not contain an emoji

I have executed the above example code and added the screenshot below.

How to Check if a String is an Emoji in Python

This function uses a regex pattern to match any emoji characters in the string. The emoji_pattern covers a wide range of emoji Unicode points.

Read How to Check if a Variable is Empty in Python?

Handle Multiple Emojis

In real-world applications, you might encounter strings with multiple emojis. The following function extracts all emojis from a string:

def extract_emojis(string):
    return emoji_pattern.findall(string)

# Example usage
string = "Hello 🇺🇸! How are you 😊?"
emojis = extract_emojis(string)
print("Emojis found:", emojis)

This function uses the findall method to return a list of all emojis in the string.

Read How to Add Two Variables in Python?

Conclusion

In this tutorial, we explored various methods to check if a string is an emoji in Python. I used Unicode ranges, the emoji library, and regular expressions to detect emojis. I also discussed how to handle multiple emojis.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.