Build a Password Checker
In this lesson, we’re going to build a password checker logic with python.
We'll create logic similar to any sign-up page where it tells you that your password is weak... We all hate it, but it's a good excercise in Python.
So, Here’s the plan:
🙋♂️ Ask for username and password
✔️ At least 8+ characters
✔️ Check for lowercase letter
✔️ Check for uppercase letter
✔️ Check for digits
✔️ Check for symbols
✔️ Check for spaces
📜 Report if password is secure enough.
Same as previously - Pause here and try to do it on your own (without ChatGPT). Then come back and let's do it together.
Ready? Set. Code 🚀
🙋♂️ Ask User Input
The first step is simple: ask the user for their username and password.
print("Creating a new account")
username = input("Enter username: ")
password = input("Enter password: ")
print('-'*50)
print("Creating a new account")
username = input("Enter username: ")
password = input("Enter password: ")
print('-'*50)
print("Creating a new account")
username = input("Enter username: ")
password = input("Enter password: ")
print('-'*50)
Sign-Up For Future Updates✨
Be among the first people to hear about
New Python Courses or Useful Resources!
Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!
Sign-Up For Future Updates✨
Be among the first people to hear about
New Python Courses or Useful Resources!
Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!
Sign-Up For Future Updates✨
Be among the first people to hear about
New Python Courses or Useful Resources!
Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!
🔒 Security Checks
Now, we need to make a series of checks.
For clarity, we'll create placeholders for each check to hold True/False values so we can print the results later on too.
check_length = False
check_digit = False
check_lower = False
check_upper = False
check_symbol = False
check_no_spaces = False
check_length = False
check_digit = False
check_lower = False
check_upper = False
check_symbol = False
check_no_spaces = False
check_length = False
check_digit = False
check_lower = False
check_upper = False
check_symbol = False
check_no_spaces = False
Now our goal is to go one by one and set it to True if it passes the test. I'll combine it into a single snippet so it's easier to read:
if len(password) >= 8:
check_length = True
if ' ' not in password:
check_no_spaces = True
for char in password:
if char.isdigit():
check_digit = True
elif char.isupper():
check_upper = True
elif char.islower():
check_lower = True
elif char in symbols:
check_symbol = True
if len(password) >= 8:
check_length = True
if ' ' not in password:
check_no_spaces = True
for char in password:
if char.isdigit():
check_digit = True
elif char.isupper():
check_upper = True
elif char.islower():
check_lower = True
elif char in symbols:
check_symbol = True
if len(password) >= 8:
check_length = True
if ' ' not in password:
check_no_spaces = True
for char in password:
if char.isdigit():
check_digit = True
elif char.isupper():
check_upper = True
elif char.islower():
check_lower = True
elif char in symbols:
check_symbol = True
As you can see it's very simple to do checks, we just need to remember a few built-in methods or membership operators to create these tests.
Also notice that we're creating a loop for a string, because it's a sequence of characters, and we can look at each character until we satisfy a condition.
Step 3: Combining the Checks
Lastly, we need to create a logical statement if everything is good or notify the user what's wrong. That's why we created variables to hold True/False about each test earlier.
We could use AND operator to combine all tests:
if check_length and check_digit and check_lower and check_upper and check_symbol and check_no_spaces:
print('✅ Account Created Successfully.')
else:
print('❌ Password is not strong enough.')
if check_length and check_digit and check_lower and check_upper and check_symbol and check_no_spaces:
print('✅ Account Created Successfully.')
else:
print('❌ Password is not strong enough.')
if check_length and check_digit and check_lower and check_upper and check_symbol and check_no_spaces:
print('✅ Account Created Successfully.')
else:
print('❌ Password is not strong enough.')
But there are too many of them and it looks ugly.
So instead, we can combine them into a list and then use all() built-in function to check if all values are True. It will look much cleaner and easier to read.
checks = [check_length,
check_digit,
check_lower,
check_upper ,
check_symbol,
check_no_spaces ]
if all(checks):
print('✅ Account Created Successfully.')
else:
print('❌ Password is not strong enough.')
checks = [check_length,
check_digit,
check_lower,
check_upper ,
check_symbol,
check_no_spaces ]
if all(checks):
print('✅ Account Created Successfully.')
else:
print('❌ Password is not strong enough.')
checks = [check_length,
check_digit,
check_lower,
check_upper ,
check_symbol,
check_no_spaces ]
if all(checks):
print('✅ Account Created Successfully.')
else:
print('❌ Password is not strong enough.')
💡 Try To Always Make Your Code More Readable!
Notify What's Wrong:
Lastly, If the password isn’t strong enough, the user should know why. Let's look at each check variable and display the message if it's False. And avoid elif in this case, because we want to make individual checks unrelated to each tother.
if all(checks):
print('✅ Account Created Successfully.')
else:
print('❌ Password is not strong enough.')
if not check_length:
print('❌ - At least 8 characters')
if not check_symbol:
print(f'❌ - At least one symbol ({symbols})')
if not check_digit:
print('❌ - At least one digit.')
if not check_upper:
print('❌ - At least one uppercase letter.')
if not check_lower:
print('❌ - At least one lowercase letter.')
if not check_no_spaces:
print('❌ - Should not contain spaces.')
if all(checks):
print('✅ Account Created Successfully.')
else:
print('❌ Password is not strong enough.')
if not check_length:
print('❌ - At least 8 characters')
if not check_symbol:
print(f'❌ - At least one symbol ({symbols})')
if not check_digit:
print('❌ - At least one digit.')
if not check_upper:
print('❌ - At least one uppercase letter.')
if not check_lower:
print('❌ - At least one lowercase letter.')
if not check_no_spaces:
print('❌ - Should not contain spaces.')
if all(checks):
print('✅ Account Created Successfully.')
else:
print('❌ Password is not strong enough.')
if not check_length:
print('❌ - At least 8 characters')
if not check_symbol:
print(f'❌ - At least one symbol ({symbols})')
if not check_digit:
print('❌ - At least one digit.')
if not check_upper:
print('❌ - At least one uppercase letter.')
if not check_lower:
print('❌ - At least one lowercase letter.')
if not check_no_spaces:
print('❌ - Should not contain spaces.')
Now the feedback is clear.
Wrapping It in a Function
Lastly, let's turn everything into a function and add doc-strings to describe our code:
def check_password(password):
"""Check the strength of your password.
Checks:
- Length (minimum 8 characters)
- Contains at least one digit
- Contains at least one uppercase letter
- Contains at least one lowercase letter
- Contains at least one special character
- Contains no spaces
Returns: True/False"""
print('-'*50)
print(f'Checking Password: {password}')
symbols = '!@#$%^&*()_+-<>/{}|'
check_length = False
check_digit = False
check_lower = False
check_upper = False
check_symbol = False
check_no_spaces = False
if len(password) >= 8:
check_length = True
if ' ' not in password:
check_no_spaces = True
for char in password:
if char.isdigit():
check_digit = True
elif char.isupper():
check_upper = True
elif char.islower():
check_lower = True
elif char in symbols:
check_symbol = True
checks = [check_length,
check_digit,
check_lower,
check_upper ,
check_symbol,
check_no_spaces ]
if all(checks):
print('✅ Account Created Successfully.')
return True
else:
print('Password is not strong enough.')
if not check_length:
print('❌ - At least 8 characters')
if not check_symbol:
print(f'❌ - At least one symbol ({symbols})')
if not check_digit:
print('❌ - At least one digit.')
if not check_upper:
print('❌ - At least one uppercase letter.')
if not check_lower:
print('❌ - At least one lowercase letter.')
if not check_no_spaces:
print('❌ - Should not contain spaces.')
return False
def check_password(password):
"""Check the strength of your password.
Checks:
- Length (minimum 8 characters)
- Contains at least one digit
- Contains at least one uppercase letter
- Contains at least one lowercase letter
- Contains at least one special character
- Contains no spaces
Returns: True/False"""
print('-'*50)
print(f'Checking Password: {password}')
symbols = '!@#$%^&*()_+-<>/{}|'
check_length = False
check_digit = False
check_lower = False
check_upper = False
check_symbol = False
check_no_spaces = False
if len(password) >= 8:
check_length = True
if ' ' not in password:
check_no_spaces = True
for char in password:
if char.isdigit():
check_digit = True
elif char.isupper():
check_upper = True
elif char.islower():
check_lower = True
elif char in symbols:
check_symbol = True
checks = [check_length,
check_digit,
check_lower,
check_upper ,
check_symbol,
check_no_spaces ]
if all(checks):
print('✅ Account Created Successfully.')
return True
else:
print('Password is not strong enough.')
if not check_length:
print('❌ - At least 8 characters')
if not check_symbol:
print(f'❌ - At least one symbol ({symbols})')
if not check_digit:
print('❌ - At least one digit.')
if not check_upper:
print('❌ - At least one uppercase letter.')
if not check_lower:
print('❌ - At least one lowercase letter.')
if not check_no_spaces:
print('❌ - Should not contain spaces.')
return False
def check_password(password):
"""Check the strength of your password.
Checks:
- Length (minimum 8 characters)
- Contains at least one digit
- Contains at least one uppercase letter
- Contains at least one lowercase letter
- Contains at least one special character
- Contains no spaces
Returns: True/False"""
print('-'*50)
print(f'Checking Password: {password}')
symbols = '!@#$%^&*()_+-<>/{}|'
check_length = False
check_digit = False
check_lower = False
check_upper = False
check_symbol = False
check_no_spaces = False
if len(password) >= 8:
check_length = True
if ' ' not in password:
check_no_spaces = True
for char in password:
if char.isdigit():
check_digit = True
elif char.isupper():
check_upper = True
elif char.islower():
check_lower = True
elif char in symbols:
check_symbol = True
checks = [check_length,
check_digit,
check_lower,
check_upper ,
check_symbol,
check_no_spaces ]
if all(checks):
print('✅ Account Created Successfully.')
return True
else:
print('Password is not strong enough.')
if not check_length:
print('❌ - At least 8 characters')
if not check_symbol:
print(f'❌ - At least one symbol ({symbols})')
if not check_digit:
print('❌ - At least one digit.')
if not check_upper:
print('❌ - At least one uppercase letter.')
if not check_lower:
print('❌ - At least one lowercase letter.')
if not check_no_spaces:
print('❌ - Should not contain spaces.')
return False
Also notice that I've added return statements if password is secure or not.
Test Multiple Password
Now that our code is wrapped into a function we can reuse it multiple times. So let's create a list of passwords to check, and then test them one by one by calling the function name.
passwords = [
"Abc%",
"trongass9!",
"P@2025",
"Secure99X",
"HelloWorld7&",
"MyPwd2025$X",
"Test$Pass9Z",
"Abcde1#23XY",
"UltraSafe",
"Zz99!XyyzW"
]
for password in passwords:
check = check_password(password)
print(f'Is Secure: {check}')
passwords = [
"Abc%",
"trongass9!",
"P@2025",
"Secure99X",
"HelloWorld7&",
"MyPwd2025$X",
"Test$Pass9Z",
"Abcde1#23XY",
"UltraSafe",
"Zz99!XyyzW"
]
for password in passwords:
check = check_password(password)
print(f'Is Secure: {check}')
passwords = [
"Abc%",
"trongass9!",
"P@2025",
"Secure99X",
"HelloWorld7&",
"MyPwd2025$X",
"Test$Pass9Z",
"Abcde1#23XY",
"UltraSafe",
"Zz99!XyyzW"
]
for password in passwords:
check = check_password(password)
print(f'Is Secure: {check}')
This will check each password one by one, giving clear feedback on how to make it more secure.
Final Thoughts
And that’s it! 🎉
Now you know how to create a Password Checker with Python.
No password can escape your security check now:
Safe, Safe, Safe, DETECTED...😂
Happy Coding!
🙋♂️ See you in the next lesson.
- EF
Happy Coding!
🙋♂️ See you in the next lesson.
- EF
Happy Coding!
🙋♂️ See you in the next lesson.
- EF