I will provide a Python program to check valid password in this article. In this program, the input given by the user is a combination of letters, digits, and special characters. So let us begin!
Topics Covered
Python program to check valid password
The password constraints for this program are as follows:
- A password length should be more than 8 characters.
- A password must contain at least one capital letter.
- A password must contain at least one small letter.
- A password must contain at least one digit.
- A password must contain at least one – ‘#’,’$’, or ‘@’.
Steps for Python program to check valid password
To do this program follow these steps:
- Accept a string as a password
- Check the length should be more than 8 characters using the if condition
- If the first condition is evaluated to be true, The password is traversed using for-loop
- In the for loop different conditions will be checked for at least 1 upper, lower, digits, and special characters. Hence I have used counters for them to count the frequency of each letter, digit or special character
- Finally, the condition is applied outside the loop for the counter more than 1 is valid password otherwise invalid password
- The output will be displayed accordingly.
Code for python program to check valid password
The code for this program is as follows:
def pass_val(pwd):
u=0
l=0
d=0
sp=0
if len(pwd)>8:
for i in pwd:
if i.isupper():
u+=1
if i.islower():
l+=1
if i.isdigit():
d+=1
if i=='@' or i=='#' or i=='$':
sp+=1
if u>=1 and l>=1 and d>=1 and sp>=1:
print("Valid Password")
else:
print("Not a valid password")
p=input("Enter password:")
pass_val(p)
Output:
Watch this video for more explanation:
Follow this link for more programs: