check email subject is emoji in python

To check if an email subject contains emojis in Python, you can use the emoji library, which provides a simple way to work with emojis. Here's an example:

main.py
import emoji

def check_emoji(subject):
    """
    Check if an email subject contains emojis.

    Args:
        subject (str): Email subject.

    Returns:
        bool: True if the subject contains emojis, False otherwise.
    """
    return emoji.is_emoji(subject) or any(emoji.is_emoji(char) for char in subject)

# Example usage:
subject = "😊 Hello World"
print(check_emoji(subject))  # Output: True

subject = "Hello World"
print(check_emoji(subject))  # Output: False
477 chars
21 lines

In this code:

  1. We import the emoji library.
  2. We define a function check_emoji that takes an email subject as input.
  3. Inside the function, we use emoji.is_emoji(subject) to check if the entire subject is an emoji. This is useful for subjects that contain only a single emoji character.
  4. We also use a generator expression any(emoji.is_emoji(char) for char in subject) to check each character in the subject individually. This is useful for subjects that contain multiple characters, including emojis.
  5. Finally, we use the any function to return True if any character is an emoji and False otherwise.

To use this code, you can install the emoji library using pip:

pip install emoji
18 chars
2 lines

related categories

gistlibby LogSnag