Python Cyber Security - Check password strength and get suggestions
Python Cyber Security: Exercise-7 with Solution
Write a Python program that creates a password strength meter. The program should prompt the user to enter a password and check its strength based on criteria such as length, complexity, and randomness. Afterwards, the program should provide suggestions for improving the password's strength.
The highest strength password that meets the criteria specified in the program will receive a score of 5.
(e.g. at least 8 characters, contains both uppercase and lowercase letters, and at least one number and one special character).
Sample Solution:
Python Code:
import re
def check_password_strength(password):
score = 0
suggestions = []
# check length
if len(password) >= 8:
score += 1
else:
suggestions.append("Password should be at least 8 characters long")
# check for uppercase letter
if re.search(r"[A-Z]", password):
score += 1
else:
suggestions.append("Password should contain at least one uppercase letter")
# check for lowercase letter
if re.search(r"[a-z]", password):
score += 1
else:
suggestions.append("Password should contain at least one lowercase letter")
# check for numeric digit
if re.search(r"\d", password):
score += 1
else:
suggestions.append("Password should contain at least one numeric digit")
# check for special character
if re.search(r"[[email protected]#$%^&*(),.?\":{}|<>]", password):
score += 1
else:
suggestions.append("Password should contain at least one special character ([email protected]#$%^&*(),.?\":{}|<>)")
return score, suggestions
password = input("Input a password: ")
print(check_password_strength(password))
Sample Output:
Input a password: Akoiuw3^*([email protected] (5, []) Input a password: [email protected] (4, ['Password should contain at least one lowercase letter']) Input a password: Ohuyt1234 (4, ['Password should contain at least one special character ([email protected]#$%^&*(),.?":{}|<>)']) Input a password: Aqwjuir (2, ['Password should be at least 8 characters long', 'Password should contain at least one numeric digit', 'Password should contain at least one special character ([email protected]#$%^&*(),.?":{}|<>)'])
Flowchart:

Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Check if passwords in a list have been leaked in data breaches.
Next: Generate password from a dictionary file.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Decapitalizes the first letter of a string:
Example:
def tips_decapitalize(s, upper_rest=False): return s[:1].lower() + (s[1:].upper() if upper_rest else s[1:]) print(tips_decapitalize('PythonTips')) print(tips_decapitalize('PythonTips', True))
Output:
pythonTips pYTHONTIPS
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook