Python Email Slicer Project

Python project 15

Python Email Slicer Project

Build an email slicer that performs practical syntax checks and displays the username, complete domain, domain name, and top-level domain. The project is a compact lesson in regular expressions, named groups, strings, and honest validation limits.

Learn Python with mentor-guided projectsView all project ideas

What you will build

The program accepts a conventional ASCII email address, rejects common formatting mistakes, splits the domain at its final dot, and explains that valid-looking syntax does not prove mailbox existence.

Skills practised

  • Regular expressions
  • Named match groups
  • String splitting
  • Validation boundaries

Requirements

  • Python 3.10 or later
  • A terminal or command prompt
  • No external packages
  • About 25 minutes to build

Full Python code

Save this code as email_slicer.py, then follow the run instructions below.

"""Split a reasonably formed email address into useful components."""

from __future__ import annotations

import re


EMAIL_PATTERN = re.compile(
    r"^(?=.{1,254}$)(?P<username>[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]{1,64})@"
    r"(?P<domain>(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+"
    r"[A-Za-z]{2,63})$"
)


def slice_email(address: str) -> dict[str, str]:
    """Return email components after practical (not deliverability) validation."""
    match = EMAIL_PATTERN.fullmatch(address.strip())
    if match is None:
        raise ValueError("Enter an address such as name@example.com.")

    username = match.group("username")
    if username.startswith(".") or username.endswith(".") or ".." in username:
        raise ValueError("Dots cannot begin, end, or repeat in the username.")

    domain = match.group("domain").lower()
    name, top_level_domain = domain.rsplit(".", 1)
    return {
        "username": username,
        "domain": domain,
        "domain_name": name,
        "top_level_domain": top_level_domain,
    }


def main() -> None:
    print("Python Email Slicer")
    address = input("Email address: ").strip()
    try:
        parts = slice_email(address)
    except ValueError as error:
        print(f"Invalid input: {error}")
        return

    print(f"Username: {parts['username']}")
    print(f"Domain: {parts['domain']}")
    print(f"Domain name: {parts['domain_name']}")
    print(f"Top-level domain: {parts['top_level_domain']}")
    print("Note: valid-looking syntax does not prove that the mailbox exists.")


if __name__ == "__main__":
    main()

How the project works

The regular expression checks useful length limits and common characters while requiring a dotted domain. A separate rule rejects usernames that begin or end with a dot or contain consecutive dots.

rsplit(".", 1) separates only the last domain label, so an address at example.co.in reports in as the top-level domain. Email standards contain more edge cases than this beginner project supports, and only a confirmation workflow can establish that a mailbox owner receives mail.

Run the project

  1. Run python email_slicer.py.
  2. Enter an address such as learner@example.com.
  3. Review the username and domain components.
  4. Try malformed addresses to observe the validation messages.

Accuracy and safety notes

  • Syntax only: this code does not send mail or verify deliverability.
  • International addresses: this beginner version intentionally accepts ASCII characters only.
  • Privacy: avoid logging or publishing real email addresses.

Ways to extend it

Add IDNA domain support, mask part of the username, process a consented CSV file, or pair syntax checks with an owner-confirmation email in a properly secured application.

Continue learning Python

Build the next project, return to the Softenant project library, or explore the Python programming course in Vizag for structured lessons, mentor feedback, and portfolio practice.