Validating Email using Python

Python is a quite simple and powerful programming language in the sense that it can be applied to so many areas like Scientific Computing, Natural Language Processing but one specific area of application of Python which I found quite fascinating is => Using it for Syntax Validations.

The Software systems which accept User-Input need to first check whether User entered what is requested or not. If there is a box which says to enter Email Address and if User entered Mobile Number, and you’ve not implemented any Validation Checking and just send that information to your software backend then it may break your software. Most famous of these is DOS(Denial of Service) attack which Hackers use for getting into servers of websites, using those pages of website which ask for User Input. (Unfortunately this have happened to some of my friend’s websites)
Anyway in order to make your Software System more reliable, secure and robust => Any kind of User Input should be validated before sending that input to Backend of Software System.

In this article, I’ll discuss about How to validate Email using Python? also same logic which I’ll be explaining in this article can be used for Validating Login System for a Python Django Website.

Let’s discuss how to validate an Email Address using Python Programming Language.

Using Python re Module for Validating Email

Below are steps for validating email using python =>

  1. First import Python’s re module into code file using import re statement
  2. Make a Regex which matches against usual email syntaxes like,,
  3. Regex for matching these Email Syntaxes is ^[a-z0-9]+[._]?[a-z0-9]+[@]\w+[.]\w{2,3}$
  4. Use re module’s search(regex, email) function for matching email addresses against Regex
  5. If search(regex, email) function returns True then email is valid, otherwise not

Let’s put all of these 5 steps together as Python Code for validating an email address =>

# Import Python's re module
import re 

# Regex for checking Email Syntax
regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'

# Valid Email checking Function
def check(email):  
		print(email, " => is valid") 
		print(emai, " => is not valid") 


Output of above code will be =>  => is valid  => is valid  => is not valid

This is the simplest approach for validating whether an email address is correct or not using Python Programming Language.

Also if in case, your not that good in understanding Regular Expressions then let me explain a little about How Regex used in above Python Code for validating Email actually works.


  • ^[a-z0-9]+ => Means start with either number or alphabet and have any of these any number of times. Like 1736gdbeyujevb, vydevuce8929878 or something like this
  • [\._]? => Means either have or not have dot/underscore. Like josh. or josh_
  • [a-z0-9]+ => Means have either number or alphabet any number of times. Like 10403hjdbece, beuceb20900
  • [@] => Must be a At Sign. Like josh@ or computersciencehub@
  • \w+ => Means have any number of just alphabets like gmail, yahoo
  • [.] => Means have a dot
  • \w{2, 3} => Means have two or three alphabetic characters like com or org

I hope that this short kind of tutorial helped you to understand How is Email Regex Expression Designed and how to validate an email address using Python Programming Language.

Comments to: Validating Email using Python

Your email address will not be published.