How to Validate Passwords in Python?

In this tutorial, I will explain how to validate passwords in Python. As a Python developer working on a project for one of our USA clients, I came across a scenario where I needed to validate passwords when dealing with user registration to make the application more secure. I will help you to learn different methods to validate passwords effectively with examples.

Methods to Validate Passwords in Python

In this article, we’ll explore various methods to check if a given string is a valid password in Python.

Method 1: Use Regular Expressions

One effective way to validate passwords in Python is by using regular expressions (regex). Regex allows you to define a pattern that the password must match. Here’s an example:

import re

def validate_password(password):
    pattern = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$"
    return re.match(pattern, password) is not None
print(validate_password("Passw0rd!"))

Output:

True

I have executed the above example code and added the screenshot below.

Validate Passwords in Python

This code uses a function that checks if a given password satisfies certain conditions defined by the regex pattern:

  • (?=.*?[A-Z]): At least one uppercase letter
  • (?=.*?[a-z]): At least one lowercase letter
  • (?=.*?[0-9]): At least one digit
  • (?=.*?[#?!@$%^&*-]): At least one special character
  • .{8,}: Minimum 8 characters long

If the password matches the pattern, the function returns True , indicating a valid password. Otherwise, it returns False.

Read How to Use the round() Function in Python?

Method 2: Custom Validation Function

Another approach is to write a custom function that checks each condition separately:

def validate_password(password):
    if len(password) < 8:
        return False
    if not any(char.isupper() for char in password):
        return False
    if not any(char.islower() for char in password):
        return False
    if not any(char.isdigit() for char in password):
        return False
    special_chars = "#?!@$%^&*-"
    if not any(char in special_chars for char in password):
        return False
    return True
print(validate_password("P@ssword"))

Output:

False

I have executed the above example code and added the screenshot below.

How to Validate Passwords in Python

This function checks if the password meets the following criteria:

  1. Minimum length of 8 characters
  2. Contains at least one uppercase letter
  3. Contains at least one lowercase letter
  4. Contains at least one digit
  5. Contains at least one special character from the set "#?!@$%^&*-"

If the password fails any of these checks, the function returns False. If it passes all checks, it returns True.

Check out How to Use the Floor() Function in Python?

Method 3: Use the passlib Library

Python’s passlib library provides a convenient way to handle password hashing and validation. Here’s an example:

from passlib.hash import pbkdf2_sha256

def validate_password(password):
    return pbkdf2_sha256.verify(password, pbkdf2_sha256.hash(password))

This code uses the pbkdf2_sha256 algorithm from passlib to hash the password and then verify if the password matches its hashed value. If the verification succeeds, the function returns True , indicating a valid password.

Read How to Use Built-In Functions in Python?

Examples

Here are some examples of using the password validation functions:

password1 = "Str0ngP@ssw0rd"
password2 = "weakpass"

print(validate_password(password1))
print(validate_password(password2))

Output:

True
False

In this example, password1 meets all the criteria for a strong password, so it returns True, while password2 is considered weak and returns False.

Check out How to Use the find() Function in Python?

Conclusion

In this tutorial, I discussed how to validate passwords in Python, We discussed various methods to validate passwords in Python like using regular expressions, custom validation functions, or libraries like passlib with examples.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.