0
0
PythonHow-ToBeginner · 3 min read

How to Validate Email Using Python: Simple Guide

You can validate an email in Python using the re module with a regular expression pattern that matches the email format. Alternatively, use the email.utils module for basic validation or third-party libraries for more complex checks.
📐

Syntax

Use the re module to match an email pattern with re.match(). The pattern is a string describing the allowed email format.

  • re.match(pattern, string): Checks if the string matches the pattern from the start.
  • pattern: A regular expression defining valid email characters and structure.
  • string: The email address to validate.
python
import re

pattern = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
email = "user@example.com"

if re.match(pattern, email):
    print("Valid email")
else:
    print("Invalid email")
Output
Valid email
💻

Example

This example shows how to check if an email is valid using a regular expression. It prints "Valid email" if the email matches the pattern, otherwise "Invalid email".

python
import re

def validate_email(email):
    pattern = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
    return bool(re.match(pattern, email))

emails = ["test@example.com", "bad-email@", "hello.world@site.co.uk"]

for e in emails:
    if validate_email(e):
        print(f"{e} is valid")
    else:
        print(f"{e} is invalid")
Output
test@example.com is valid bad-email@ is invalid hello.world@site.co.uk is valid
⚠️

Common Pitfalls

Common mistakes include:

  • Using too simple regex that allows invalid emails or blocks valid ones.
  • Not anchoring the regex with ^ and $, causing partial matches.
  • Ignoring domain validation (e.g., multiple dots or invalid characters).
  • Assuming regex can catch all invalid emails; some require sending confirmation emails.
python
import re

# Wrong: missing anchors, allows partial matches
pattern_wrong = r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+"

email = "bad-email@"

if re.match(pattern_wrong, email):
    print("Wrongly accepted")
else:
    print("Correctly rejected")

# Right: anchors added
pattern_right = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"

if re.match(pattern_right, email):
    print("Wrongly accepted")
else:
    print("Correctly rejected")
Output
Wrongly accepted Correctly rejected
📊

Quick Reference

Tips for email validation in Python:

  • Use re.match() with a proper regex pattern anchored by ^ and $.
  • Remember regex checks format, not if the email actually exists.
  • For simple validation, email.utils.parseaddr() can help extract emails.
  • For robust validation, consider libraries like validate_email or sending confirmation emails.

Key Takeaways

Use the re module with a proper regex pattern to validate email format in Python.
Always anchor your regex with ^ and $ to avoid partial matches.
Regex validation checks format only; it does not verify if the email exists.
Consider third-party libraries or confirmation emails for stronger validation.
Simple built-in tools like email.utils can help parse but not fully validate emails.