w3resource

Python Exercise: Check the validity of a password

Python Conditional: Exercise-15 with Solution

Write a Python program to check the validity of a password (input from users).

Validation :

  • At least 1 letter between [a-z] and 1 letter between [A-Z].
  • At least 1 number between [0-9].
  • At least 1 character from [$#@].
  • Minimum length 6 characters.
  • Maximum length 16 characters.

Sample Solution:-

Python Code:

import re
p= input("Input your password")
x = True
while x:  
    if (len(p)<6 or len(p)>12):
        break
    elif not re.search("[a-z]",p):
        break
    elif not re.search("[0-9]",p):
        break
    elif not re.search("[A-Z]",p):
        break
    elif not re.search("[$#@]",p):
        break
    elif re.search("\s",p):
        break
    else:
        print("Valid Password")
        x=False
        break

if x:
    print("Not a Valid Password")

Sample Output:

Input your [email protected]                                                                                   
Valid Password 

Flowchart:

Flowchart: Python: Check the validity of a password

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program that accepts a string and calculate the number of digits and letters.
Next: Write a Python program to find numbers between 100 and 400 (both included) where each digit of a number is an even number. The numbers obtained should be printed in a comma-separated sequence.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Returns a list with n elements removed from the beginning

Example:

def tips_take(itr, n = 1):
  return itr[:n]
print(tips_take([1, 2, 3], 5))
print(tips_take([1, 2, 3], 0))

Output:

[1, 2, 3]
[]