Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Normalize and validate an email address

Clean and validate an email string, returning a normalized form or None for invalid inputs.

Python practice13 minError Handling & Edge CasesIntermediateLast updated March 20, 2026

Problem statement

Write a function normalize_email(email) that takes an email string and returns a normalized email string when the input is valid, or None when invalid. Normalization rules: - Strip leading/trailing whitespace and lowercase the whole address. - If the domain is gmail.com or googlemail.com, remove dots in the local part and remove any plus-tag (text starting with '+') before the '@'. For other domains, leave the local part (except trimming and lowercasing). Validation rules (apply after trimming and lowercasing): - There must be exactly one '@'. - Local part must be non-empty and cannot start or end with '.' - Domain must contain at least one '.' separating labels; labels must start and end with an alphanumeric character (letters or digits). - The email must not contain spaces. Return the normalized email string if valid, otherwise return None.

Task

Implement a function that defensively normalizes emails (strip, lowercase, optional Gmail-specific cleanup) and validates format.

Examples

Gmail normalization

Input

normalize_email('User.Name+tag@gmail.com')

Output

'username@gmail.com'

Gmail domain: dots removed and plus-tag removed; result lowercased.

Generic domain lowercasing

Input

normalize_email(' Admin@Example.COM ')

Output

'admin@example.com'

Trim spaces and lowercase; local part kept as-is for non-gmail domains.

Input format

A single string argument: the email to normalize.

Output format

Return a normalized email string if valid, otherwise return None.

Constraints

- Do not use external libraries. Keep checks simple and defensive. - Emails may contain unusual inputs; function must not raise exceptions for malformed inputs.

Samples

Sample 1

Input

normalize_email('john.doe+newsletter@googlemail.com')

Output

'johndoe@googlemail.com'

Googlemail is treated like gmail: dots and plus-tags removed.