Change language

Check if email address is correct or not in Python

|

When you receive the string, write a Python program to check if the string is a valid email address or not.

Email — it is a string (a subset of ASCII characters), separated into two parts by the @ symbol, "personal_info" and the domain, that is, personal_info @ domain.

Examples :

  Input:  [email protected]  Output:  Valid Email  Input:  [email protected]  Output:  Valid Email  Input:  ankitrai326.com  Output:  Invalid Email 

In this program we use the search () method of the re module ... so let’s see a description about that.

re.search (): this method either returns None (if the pattern doesn’t match) or re.MatchObject which contains information about the corresponding part of the string. This method stops after the first match, so it is better suited for validating regular expressions than extracting data.

Let’s take a look at a Python email validation program:

# Python Email Checker

 
# import re module

 
# re module provides support
# for regular expressions

import re

 
# Make a regular expression
# to check email

regex = ’ ^ w + ([.-]? w +) * @ w + ([.-]? w +) * (. w {2,3}) + $ ’

  
# Define a function for
# to check email

def check (email): 

 

# pass a regular expression

# and a string in the search () method

if (re.search (regex, email)) : 

print ( "Valid Email"

 

< code class = "keyword"> else

print ( " Invalid Email "

 

 
Driver code

if __ name__ = = ’__main__’

 

# Enter your email address

email = "[email protected]"

 

# call the run function

check (email)

 

email = "[email protected]"

  check (email)

  

email = "ankitrai326 .com "

  check (email)

Exit:

 Valid Email Valid Email Invalid Email 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically