A python program to check valid password

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!

Python program to check valid password

The password constraints for this program are as follows:

  1. A password length should be more than 8 characters.
  2. A password must contain at least one capital letter.
  3. A password must contain at least one small letter.
  4. A password must contain at least one digit.
  5. A password must contain at least one – ‘#’,’$’, or ‘@’.

Steps for Python program to check valid password

To do this program follow these steps:

  1. Accept a string as a password
  2. Check the length should be more than 8 characters using the if condition
  3. If the first condition is evaluated to be true, The password is traversed using for-loop
  4. 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
  5. Finally, the condition is applied outside the loop for the counter more than 1 is valid password otherwise invalid password
  6. 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:

python program to check valid password - output

Watch this video for more explanation:

Follow this link for more programs:

Python program to swap string parts

Leave a Reply