Python
Python

 

Introduction

In today’s digital world, email addresses are an essential part of communication. However, ensuring that an email address is properly formatted is equally important for preventing errors or ensuring smooth functionality of email-based services. This program aims to validate whether a given string is a correctly formatted email address by checking various patterns such as the presence of the “@” symbol, a domain name, and valid characters.

Objective

The goal of this Python program is to implement a function that checks if an input string follows the proper syntax of an email address. It will use regular expressions to match the structure of a valid email address, which includes the username, ‘@’ symbol, and domain with a valid extension.

Python Code

import re

# Function to validate an email address using regex
def is_valid_email(email):
    # Regular expression pattern for validating an email
    email_pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
    
    # Using re.match to check if the email matches the pattern
    if re.match(email_pattern, email):
        return True
    else:
        return False

# Main code to take user input and validate the email
if __name__ == "__main__":
    email = input("Enter the email address to validate: ")
    
    if is_valid_email(email):
        print(f"The email address '{email}' is valid.")
    else:
        print(f"The email address '{email}' is not valid.")

Explanation of the Program

The program uses Python’s built-in re module, which provides support for working with regular expressions. The function is_valid_email takes an email string as input and applies a regular expression pattern that matches typical email formats.

Here’s a breakdown of the email validation regular expression:

  • ^[a-zA-Z0-9_.+-]+: The email’s local part (before the @ symbol) must contain one or more alphanumeric characters or special characters such as ._+-.
  • @: The “@” symbol separates the local part from the domain part.
  • [a-zA-Z0-9-]+: The domain name (after the @ symbol) can contain alphanumeric characters or hyphens.
  • \.[a-zA-Z0-9-.]+$: The domain extension must begin with a dot followed by alphanumeric characters or hyphens. The extension can have multiple periods (for example, .co.uk).

The program takes the user’s email input, validates it using the regular expression, and outputs whether the email address is valid or not.

How to Run the Program

Follow these steps to run the program:

  1. Install Python (if not already installed) from Python Official Website.
  2. Save the Python code to a file (e.g., email_validation.py).
  3. Open a terminal or command prompt.
  4. Navigate to the directory where the file is saved.
  5. Run the program by typing the command: python email_validation.py.
  6. Enter an email address when prompted to check its validity.
© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)