Thursday, 20 August 2026

๐Ÿš€ Day 102/150 – Email Validation Program in Python

 

๐Ÿš€ Day 102/150 – Email Validation Program in Python

Email validation is a common task in many applications such as registration forms, login systems, and contact forms. A valid email address should follow a proper format, such as containing an @ symbol, a domain name, and a valid extension.

In this post, we'll explore four different ways to validate an email address in Python.


Method 1 – Basic Email Validation

Check whether the email contains both @ and ..

password = input("Enter your password: ") special = "!@#$%^&*()_+-=[]{}|;:',.<>?/" if (len(password) >= 8 and any(char.isupper() for char in password) and any(char.islower() for char in password) and any(char.isdigit() for char in password) and any(char in special for char in password)): print("Strong Password") else: print("Weak Password")







Sample Input

user@example.com

Output
Valid Email

Explanation

  • input() reads the email address.

  • The program checks if the email contains both @ and ..

  • If both are present, it considers the email valid.

  • Otherwise, it prints "Invalid Email".


Method 2 – Check Email Format

Ensure the email contains exactly one @ and ends with a common domain extension.


email = input("Enter your email: ") if email.count("@") == 1 and email.endswith((".com", ".org", ".net")): print("Valid Email") else: print("Invalid Email")







Sample Input
python@gmail.com

Output

Valid Email

Explanation

    count("@") ensures there is only one @.
    endswith() checks if the email ends with .com, .org, or .net.
    Both conditions must be true for the email to be valid.

Method 3 – Using Regular Expressions

Use Python's re module for more accurate email validation.

import re email = input("Enter your email: ") pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" if re.match(pattern, email): print("Valid Email") else: print("Invalid Email")










Sample Input
hello123@gmail.com

Output

Valid Email

Explanation

  • The re module provides support for regular expressions.

  • re.match() checks whether the email matches the specified pattern.

  • This method is more reliable than checking only for @ and ..


Method 4 – Validate Multiple Email Addresses

Check several email addresses stored in a list.


emails = [ "alice@gmail.com", "bob@yahoo", "charlie@example.com" ] for email in emails: if "@" in email and "." in email: print(email, "- Valid") else: print(email, "- Invalid")











Output
alice@gmail.com - Valid 
bob@yahoo - Invalid 
charlie@example.com - Valid

Explanation

  • A list of email addresses is created.

  • The for loop checks each email one by one.

  • Emails containing both @ and . are marked as valid.

  • Others are marked as invalid.


Comparison of Methods

MethodBest For
Basic ValidationBeginners learning string operations
Format CheckSimple real-world validation
Regular ExpressionsAccurate email validation
Multiple EmailsValidating lists of email addresses

๐Ÿ”ฅ Key Takeaways

  • Email validation helps ensure users enter properly formatted email addresses.

  • Basic validation checks for the presence of @ and ..

  • count() and endswith() provide additional format checks.

  • The re module offers a more robust way to validate email addresses using regular expressions.

  • Email validation is commonly used in registration forms, login systems, and web applications.

Stay tuned for Day 103 of the #150DaysOfPython series! ๐Ÿš€

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (336) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (336) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (88) Coursera (302) Cybersecurity (34) data (10) Data Analysis (46) Data Analytics (31) data management (16) Data Science (419) Data Strucures (18) Deep Learning (215) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) Git (13) Google (54) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (386) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (34) Python (1360) Python Coding Challenge (1223) Python Mathematics (10) Python Mistakes (51) Python Quiz (605) Python Tips (100) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (19) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)