Skip to main content

How to check if a string is a valid email address in Python

How to check if a string is a valid email address in Python.

Here is a detailed step-by-step tutorial on how to check if a string is a valid email address in Python:

Step 1: Import the necessary modules

The first step is to import the necessary modules to work with regular expressions and email validation. In this case, we'll need the re module for regular expressions and the validate_email function from the validate_email module. To import these modules, add the following code at the beginning of your Python script:

import re
from validate_email_address import validate_email

Step 2: Define a function to check for a valid email address

Next, we'll define a function that takes a string as input and checks if it is a valid email address. This function will use regular expressions to match the pattern of a valid email address. Here's an example implementation:

def is_valid_email(email):
# Define the regular expression pattern for a valid email address
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'

# Use the re module to match the pattern against the input email
if re.match(pattern, email):
return True
else:
return False

In this example, the regular expression pattern ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ is used to match the pattern of a valid email address. This pattern checks for the following conditions:

  • The email address must start with one or more alphanumeric characters, periods, underscores, percent signs, plus signs, or hyphens.
  • The email address must contain the "@" symbol.
  • The domain name must consist of one or more alphanumeric characters, periods, or hyphens.
  • The domain name must end with a period followed by two or more alphabetic characters.

Step 3: Test the function with sample email addresses

To test the is_valid_email function, you can pass different email addresses as input and check if the function returns True for valid email addresses and False for invalid ones. Here are a few examples:

print(is_valid_email("[email protected]"))  # Output: True
print(is_valid_email("jane@example")) # Output: False
print(is_valid_email("[email protected]")) # Output: False

In the first example, the email address "[email protected]" is valid, so the function will return True. In the second example, the email address "jane@example" is missing the domain extension, so the function will return False. In the third example, the email address "[email protected]" has an extra period before the domain extension, so the function will also return False.

Additional Example: Using the validate_email function

If you prefer to use a pre-built function for email validation, you can use the validate_email function from the validate_email_address module. Here's an example implementation:

def is_valid_email(email):
return validate_email(email)

In this example, the is_valid_email function simply calls the validate_email function and returns its result. The validate_email function returns True if the email address is valid and False otherwise.

Conclusion

In this tutorial, you learned how to check if a string is a valid email address in Python. You can use the re module to match the pattern of a valid email address using regular expressions, or you can use the validate_email function from the validate_email_address module for a pre-built solution. By applying these techniques, you can ensure that your Python code only accepts valid email addresses as input.