---
title: "Python Mini Project: How to Validate Email"
description: "Here are the best examples to validate email in Python. One is writing code in for and if loops and the other is to validate regular expressions."
url: https://srinimf.com/2023/08/07/python-mini-project-how-to-validate-email/
date: 2023-08-07
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2023/08/pexels-photo-17735089.jpeg?fit=1880%2C1253&ssl=1
categories: ["Python"]
tags: ["Python Projects", "Validate Email"]
type: post
lang: en
---

# Python Mini Project: How to Validate Email

Here are two ways to validate email addresses [in Python](https://srinimf.com/2021/10/25/exclusive-loops-vs-iterator-object-in-python/). One approach is by using loops and IF conditions, and the other one is using [regular expression](https://srinimf.com/2023/04/24/6-essential-python-regular-expression-examples/)s (regex) with the re-module.

## **#1 Use For and If conditions**

To summarize, it asks the user to supply an email address. It validates conditions with the help of Python’s for loops and if conditions. Accordingly, it prints the result.

Related[The Amazing Fact Why Can’t We use Loops in Python Lambda](https://srinimf.com/2022/05/30/amazing-fact-why-cant-we-use-for-in-python-lambda/)

```
email=input("enter your email")
k=0
if len(email) >= 6:
    if email[0].isalpha():
        if ("@" in email) and (email.count("@") ==1):
            if (email[-4]== ".") ^ (email[-3] =="."):
                   for i in email:
                        if i.isspace():
                            k=1
                            
                   if k==1:
                        print("wrong email5")
            else:
                print("wrong email 4")
        else:
            print("wrong email3")
    else:
        print("wrong email2")
else:
    print("wrong email1")
```

output

```
enter your email sri.com
wrong email3
```

Advertisements

## **#2 Using regular expression**

Alternatively, using the regular expression’s search function you can validate whether the given email follows the specified pattern or not. Further, the below regular expressions have the following meanings.

Related[Breadth-first-search in AI Mini Project](https://srinimf.com/2018/01/17/ai-breadth-first-method-to-solve-complex-problems/)

^[a-z]+: The first letter one or more must be in a to z.

[\._]?: Should have either period or underscore

+: Should have one or more a to z or 0 to 9.

[@\w]\: One @ word

[.]\: One period

\w{2,3}$: Two or three words at the end.

```
import re

email_condition="^[a-z]+[\._]?[a-z 0-9]+[@\w]+[.]\w{2,3}$"
user_input=input("enter email")
if re.search(email_condition, user_input):
    print("Right Email")
else:
    print("Wrong email")
```

Output

```
enter email srini@gmail.comRight Email
```

Related

- [How to Use Conditions in Python IF Logic](https://srinimf.com/2023/03/14/how-to-use-condition-statement-in-if-logic-in-python/)DateMarch 14, 2023
- [7 Daily Use Python String Functions](https://srinimf.com/2022/02/19/daily-use-python-string-functions/)DateFebruary 19, 2022
- [How to Use For, While, and Nested Loops in Python](https://srinimf.com/2019/10/24/python-loop-control-statements-real-syntax/)DateOctober 24, 2019

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2023%2F08%2F07%2Fpython-mini-project-how-to-validate-email%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2023%2F08%2Fpexels-photo-17735089.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=Python%20Mini%20Project%3A%20How%20to%20Validate%20Email)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2023/08/07/python-mini-project-how-to-validate-email/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2023/08/07/python-mini-project-how-to-validate-email/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2023/08/07/python-mini-project-how-to-validate-email/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2023/08/07/python-mini-project-how-to-validate-email/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Python%20Mini%20Project%3A%20How%20to%20Validate%20Email&body=https%3A%2F%2Fsrinimf.com%2F2023%2F08%2F07%2Fpython-mini-project-how-to-validate-email%2F&share=email)
-

### Like this:

Like Loading…