Sometimes you need to quickly check how secure a password is. Let's look at a simple example using regular expressions - a good opportunity to practice with
re and conditional logic.Import the module:
import re
Create a password check function:
def check_password_strength(password):
length = len(password) >= 8
upper = re.search(r"[A-Z]", password)
lower = re.search(r"[a-z]", password)
digit = re.search(r"\d", password)
special = re.search(r"[@$!%*?&]", password)
if all([length, upper, lower, digit, special]):
return "✅ Reliable password"
else:
return "⚠️ Weak password"
Check a few examples:
print(check_password_strength("Qwerty123"))
print(check_password_strength("Qw!8zYt@1"))Output example:
⚠️ Weak password
✅ Reliable password
🔥 Example of how to check a string for compliance with several conditions using code - and practice with regular expressions.
🚪 @DataScience4