w3resource

Python Cyber Security - Check password strength

Python Cyber Security: Exercise-3 with Solution

Write a Python program to check if a password meets the following criteria:

At least 8 characters long and

Contains at least one uppercase letter, one lowercase letter, one digit, and one special character (!, @, #, $, %, or &)

If the password meets the criteria, print a message that says "Valid Password." If it doesn't meet the criteria, print a message that says "Password does not meet requirements."

Sample Solution:

Python Code:

# Solution to Exercise 3
import re

def validate_password(password):
    # Check if the password has at least 8 characters
    if len(password) < 8:
        return False
    
    # Check if the password contains at least one uppercase letter
    if not re.search(r'[A-Z]', password):
        return False
    
    # Check if the password contains at least one lowercase letter
    if not re.search(r'[a-z]', password):
        return False
    
    # Check if the password contains at least one digit
    if not re.search(r'\d', password):
        return False
    
    # Check if the password contains at least one special character
    if not re.search(r'[!@#$%^&*(),.?":{}|<>]', password):
        return False
    
    # If all the conditions are met, the password is valid
    return True

password = input("Input your password: ")
is_valid = validate_password(password)

if is_valid:
    print("Valid Password.")
else:
    print("Password does not meet requirements.")

Sample Output:

Input your password:  Aji1#der
Valid Password.

Input your password:  A123d$%h
Valid Password. 

Input your password:  A12&ki1
Password does not meet requirements.

Input your password:  KI342$&H
Password does not meet requirements.

Flowchart:

Flowchart: Check password strength

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Generate random passwords of specified length.
Next: Generate random passwords of specified length.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/cybersecurity/python-cybersecurity-exercise-3.php