DEV Community

Joseph Abu
Joseph Abu

Posted on

python code to verify emails

import re

def is_valid_email(email):
    # regular expression pattern for email validation
    pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'

    # match the email pattern with regular expression
    match = re.match(pattern, email)

    # if match found, return True else False
    if match:
        return True
    else:
        return False
Enter fullscreen mode Exit fullscreen mode

To use this code, simply call the is_valid_email function with the email address as a string argument. For example:

email = "example@email.com"
result = is_valid_email(email)

if result:
    print("Email is valid.")
else:
    print("Email is not valid.")
Enter fullscreen mode Exit fullscreen mode

This code uses regular expressions to match the pattern of a valid email address. The regular expression pattern includes the following rules:

The email address must start with one or more alphanumeric characters, followed by a dot, underscore, percent sign, plus sign, or hyphen. This can be repeated one or more times.
The email address must contain the at symbol (@).
The email address must contain one or more alphanumeric characters or hyphens, followed by a dot and two or more alphabetic characters. This can be repeated one or more times.
By using regular expressions, this code provides a simple and efficient way to validate email addresses in Python.

Top comments (0)