Python program to Check the Validity of a Password
Given a password string, the task is to check whether it is valid or not based on a set of rules. A valid password must follow these conditions:
- Minimum 8 characters
- At least one lowercase letter [a-z]
- At least one uppercase letter [A-Z
- At least one digit [0-9]
- At least one special character from: _ @ $
- No spaces allowed
For Examples:
Input: R@m@_f0rtu9e$
Output: Valid Password
Input: Rama_fortune$
Output: Invalid Password
Explanation: Number is missing
Let's explore different methods to validate the password.
Using a Single Regex Pattern
This method uses one complete regular expression that checks all password rules at once. If the whole password matches the pattern, it is valid, otherwise invalid.
import re
password = "R@m@_f0rtu9e$"
pattern = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[_@$])[A-Za-z\d_@$]{8,}$"
if re.fullmatch(pattern, password):
print("Valid Password")
else:
print("Invalid Password")
Output
Valid Password
Explanation:
- re.fullmatch(pattern, password): Ensures the entire string follows all rules together.
- (?=.*[a-z]) password must contain at least one lowercase letter, (?=.*[A-Z]) must contain one uppercase letter.
- (?=.*\d) must contain one digit, (?=.*[_@$]) must include one of the required special characters and {8,} ensures length is at least 8.
Using Multiple Regex Checks
Instead of one long regex, this method checks each rule separately using re.search(). Validation succeeds only if all checks pass.
import re
password = "R@m@_f0rtu9e$"
if (len(password) >= 8
and re.search("[a-z]", password)
and re.search("[A-Z]", password)
and re.search("[0-9]", password)
and re.search("[_@$]", password)
and not re.search("\s", password)):
print("Valid Password")
else:
print("Invalid Password")
Output
Valid Password
Explanation:
- re.search("[a-z]", password) checks if at least one lowercase letter exists whereas, re.search("[A-Z]", password) checks for required uppercase letter.
- re.search("[0-9]", password) checks for digit whereas, re.search("[_@$]", password) ensures at least one of the allowed special characters exists.
- not re.search("\s", password) ensures no spaces whereas, len(password) >= 8 ensures required length.
Using Built-in Character Methods (islower, isupper, isdigit)
This method loops through the password once and uses Python's character methods to count lowercase letters, uppercase letters, digits, and allowed special symbols.
l = u = p = d = 0
password = "R@m@_f0rtu9e$"
if len(password) >= 8:
for ch in password:
if ch.islower():
l += 1
if ch.isupper():
u += 1
if ch.isdigit():
d += 1
if ch in "_@$":
p += 1
if l >= 1 and u >= 1 and p >= 1 and d >= 1 and l+u+d+p == len(password):
print("Valid Password")
else:
print("Invalid Password")
Output
Valid Password
Explanation:
- ch.islower() detects lowercase characters and ch.isupper() detects uppercase
- ch.isdigit() detects digits, ch in "_@$" checks allowed special characters and l + u + p + d == len(password) ensures no invalid characters present