Check if a String is an Email in Python

  • Post category:String

In this article, you will see how to check if a string is an email in Python. An email string is a string that contains an email address with the @ sign. We can use count() along with this strip() method to check if a string contains an @ sign.

Step 1: Create a string

str = "demo_email@gmail.com"
print("str = ", str)
print("str type = ", type(str))

Step 2: Check if string is a email

if str.strip().count("@") == 1:
    print("Email")
else:
    print("Not Email")

Output:

str =  demo_email@gmail.com
str type =  <class 'str'>
Email

Free resources to learn advanced skills: AiHints and CodeAllow