Python program to validate password

Python Methods and Functions | Regular Expressions

Basic conditions for password verification:

  1. At least 8 characters.
  2. Letters must be between [az]
  3. At least one alphabet must be uppercase [AZ]
  4. At least 1 digit or digit between [0-9].
  5. At least 1 character from [_ or @ or $].

Examples:

 Input: R @ m @ _f0rtu9e $ Output: Valid Password Input: Rama_fortune $ Output: Invalid Password Explanation: Number is missing Input: Rama # fortu9e Output: Invalid Password Explanation: Must consist from _ or @ or $ 

Here we have used the re module which provides support for regular expressions in Python . Along with this the re.search () method returns False (if the first parameter is not found in the second parameter). This method is best for validating a regular expression rather than extracting data. We used re.search () to check if alphabets, numbers or special characters are correct. To check for spaces, we use "/ s" which is included in the regular expression module.

Output:

 Valid Password 




Get Solution for free from DataCamp guru

# Python program to check if the password is correct
# Regular expression module used with search ()

import re

password = "R @ m @ _f0rtu9e $ "

flag = 0

while True

if ( len (password) & lt; 8 ):

  flag = - 1

  break

elif not re.search ( "[az]" , password):

flag = - 1

  break

elif not re.search ( "[AZ]" , password):

  flag = - 1

break

elif not re.search ( "[0-9]" , password):

flag = - 1

break

  elif not re.search ( "[_ @ $]" , password):

  flag - 1

break

elif re.search ( "s" , password):

flag = - 1

break

else :

flag = 0

print ( "Valid Password"

break

 

if flag = = - 1 :

print ( "Not a Valid Password" )