Check if email address is correct or not in Python

Python Methods and Functions | Regular Expressions

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:  Output:  Invalid Email 

In this program we use the search () method of the re module ... so let's see a description about that. (): 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 ( (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)


 Valid Email Valid Email Invalid Email 

Get Solution for free from DataCamp guru