Python program to check the validity of a password

 

Python program to check the validity of a password using regular expressions

Problem Definition

Develop a python program to check the validity of a password. The password should contain at least, i) one lower case letter, ii) one digit, iii) one uppercase letter iv) one special character [$@#!] v) six characters long. The program should accept a password and check the validity of the password using the above criteria and print “Valid” and “Invalid” as the case may be.

Video Tutorial for Python program to check the validity of a password

Solution (Algorithm)

First import re so that you can regular expression and function from re library.

Set flag to 0, which indicates the read password is invalid. Next, ask the user to enter the password using the input statement of python. Store the password in passwd variable.

Use if conditional statement to check for each constraint for the validity of the password. If any of the conditions are not satisfied then set the flag to 1. For each condition write a regular expression. Also, use the search() function of re to check whether the string (password) contains the pattern. If the pattern is not present then set the flag to 1.

re.search(‘[0-9]’, passwd) checks whether the password contains a digit, if not set flag to 1.

The re.search(‘[a-z]’, passwd): checks whether the password contains a lowercase letter, if not set the flag to 1.

Next, re.search(‘[A-Z]’, passwd): checks whether the password contains an uppercase letter, if not set the flag to 1.

re.search(‘[$@#!]’, passwd): checks whether the password contains a special character, if not set the flag to 1.

Also len(passwd)<6, check whether the length of the password is less than 6, if yes set the flag to 1.

Finally, check the value of the flag, if the value of the flag is 0 then the read password is valid otherwise the password is invalid. Display the result to the user.

Program (Source code) to check the validity of a password in python

import re
flag = 0
passwd = input ("Enter the password: ")
if not re.search('[0-9]', passwd):
    flag = 1

if not re.search('[a-z]', passwd):
    flag = 1

if not re.search('[A-Z]', passwd):
    flag = 1
    
if not re.search('[$@#!]', passwd):
    flag = 1

if len(passwd)<6:
    flag = 1
    
if (flag == 0):
    print ('Password is valid')
else:
    print ('Password is invalid')

Output

Case 1:

Enter the password: M@1
Password is invalid

Case 2:

Enter the password: M@hesh123
Password is valid

Case 3:

Enter the password: mahesh123
Password is invalid

Case 4:

Enter the password: M@he1
Password is invalid

Summary:

This tutorial discusses how to build a Python program to check the validity of a password using regular expressions. Do share the tutorial with your friends and have any issues do comment in the comment box.

Leave a Comment

Your email address will not be published. Required fields are marked *

Welcome to VTUPulse.com


Computer Graphics and Image Processing Mini Projects -> Click Here

Download Final Year Project -> Click Here

This will close in 12 seconds