
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to validate email address
Suppose we have an email address as string. We have to check whether this is valid or not based on the following conditions −
The format must be username@company.domain format
Username can only contain upper and lowercase letters, numbers, dashes and underscores
Company name can only contain upper and lowercase letters and numbers
Domain can only contain upper and lowercase letters
Maximum length of the extension is 3.
We can use regular expression to validate the mail addresses. Regular expressions can be used by importing re library. To match a pattern we shall use match() function under re library.
So, if the input is like s = "popular_website15@comPany.com", then the output will be True
To solve this, we will follow these steps −
- pat := "starting with [a-zA-Z0-9-_] then @ then company name with [a-zA-Z0-9] then separated by dot and domain with [a-z] whose length is 1 to 3 and this is present at end"
- if pat matches with s, then
- return True
- otherwise return False
Example
Let us see the following implementation to get better understanding
import re def solve(s): pat = "^[a-zA-Z0-9-_]+@[a-zA-Z0-9]+\.[a-z]{1,3}$" if re.match(pat,s): return True return False s = "popular_website15@comPany.com" print(solve(s))
Input
"popular_website15@comPany.com"
Output
True
- Related Articles
- Validate Email address in Java
- How to validate email address in JavaScript?
- How to validate an email address in C#?
- Python program to validate postal address format
- How to validate an email address in PHP ?\n
- How to validate email address using RegExp in JavaScript?
- How to validate an email address using Java regular expressions.
- How to validate Email Address in Android on EditText using Kotlin?
- Validate IP Address in Python
- C Program to validate an IP address
- How to validate email using jQuery?
- How to validate an email id using regular expression in Python?
- How to validate URL address in JavaScript?
- Validate IP Address in C#
- Validate IP Address in C++
