Check whether password is in the standard format or not in Python

In this post, we will check whether a password is in the standard format or not using regular expressions in Python. If you are new to regular expressions, then here is the link for Introduction to Regular Expression in Python. The main goal of this post is to clear the concepts and provide a different view of the problems.
A password will be standard only if;

  1. The length of the password lies in the range of 8 to 16 characters.
  2. Consists of at least one special character [!@#$%^&*()-+].
  3. Consists of at least one lowercase alphabet and one uppercase alphabet.

Python program to check whether the password is in standard format or not using REs

Before implementing RegEx, firstly we need to import “re module”.

import re

Now we need to create the regex pattern for validating the input password. For this, we will use 5 regex patterns. One pattern for checking the length of the password and verifying that password does not have any white space. While the rest of the 4 regex patterns will validate each condition.

password = input()
re_pattern = "^[A-Z0-9a-z!@#$%^&*()-+]{8,16}$"  #1
re_pattern1 = "[A-Z]+"
re_pattern2 = "[a-z]+"
re_pattern3 = "[0-9]+"
re_pattern4 = "[!@#$%^&*()-+]+"
flag = 0

The benefit of “#1” is we can exclude or include any character and can define a specific length or a range.

Suppose you want the password to be exact of 8 characters only and don’t want any alphabet from “j” to “t”;
regex_pattern = "*[A-IU-Za-iu-z0-9!@#$%^&*()-+]{8}$"

For the rest of the conditions, we can use the same regex pattern.

Now to verify the conditions we will use nested if-conditional statements. Since we are using nested statements, our line of code will exceed due to else statements. So, to handle this we will have used the flag variable which will indicate to us at the end of the code whether the input password fulfills the conditions.

if bool(re.match(re_pattern , pwd)):                          #2.1
    if bool(re.search(re_pattern1 , pwd)):                    #2.2
        if bool(re.search(re_pattern2, pwd)):                 #2.3
            if bool(re.search(re_pattern3 , pwd)):            #2.4
                if bool(re.search(re_pattern4 , pwd)):        #2.5
                    flag = 1

 In #2.1, we are checking for the length and verifying that only the alphabets, numbers, and specified symbols are present and nothing from outside them. While lines #2.2 to #2.5 will check that there is at least one character of each category(uppercase and lowercase alphabets, numbers, and special symbols).
The bool() converts the result from match() and search() functions to their respective boolean values.

Notice that in line #2.1, the match() function is used while for the rest of the conditions search() function is used. The reason behind it is, match function starts matching the string from the beginning while the search function searches the whole string for the pattern.
The flow of code is such that when every condition is satisfied we have updated the flag to 1.

The same can be implemented without using nested conditions but it makes code less presentable.

if (bool(re.match(re_pattern , pwd)) and bool(re.search(re_pattern1 , pwd)) and bool(re.search(re_pattern2, pwd)) andbool(re.search(re_pattern3 , pwd)) and bool(re.search(re_pattern4 , pwd))):
    print("Valid")

CODE

import re
password = input()
flag=0

re_pattern = "^[A-Z0-9a-z!@#$%^&*()-+]{8,16}$"
re_pattern1 = "[A-Z]+"
re_pattern2 = "[a-z]+"
re_pattern3 = "[0-9]+"
re_pattern4 = "[!@#$%^&*()-+]+"

if bool(re.match(re_pattern , password)):
    if bool(re.search(re_pattern1 , password)):
        if bool(re.search(re_pattern2, password)):
            if bool(re.search(re_pattern3 , password)):
                if bool(re.search(re_pattern4 , password)):
                    flag=1
if flag:
    print("Valid")
else:
    print("Invalid")

OUTPUT

123asdFG!
Valid

123asA!
Invalid

As23!@#asd2
Valid

CodeSpeedy@12
Valid

CodeSpeedy@1234567890
Invalid

hello WOR@2d
Invalid

Also, Check out these to know more about REs;

  1.  Introduction to regular expressions and a sample problem in Python
  2. Check if a string is a valid IP address or not in Python using regular expression

Leave a Reply

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