Change language

Password checking in Python

|

Conditions for a valid password:

  1. Must have at least one number.
  2. Must have at least one uppercase and one lowercase letter.
  3. Must have at least one special character.
  4. Must be 6 to 20 characters.
  Input:  Geek12 #  Output:  Password is valid.  Input:  asd123  Output:  Invalid Password !! 

We can check if a given string can be a password or not using several methods.

Method # 1: Naive method (no regular expressions).

# Password check in Python
# using the naive method

 
# Function to check the password

def password_check (passwd):

 

SpecialSym = [ ’$’ , ’@’ , ’ # ’ , ’% ’ ]

val = True

 

if len (passwd) & lt;  6 :

print ( ’length should be at least 6’ )

val = False

 

  if len (passwd)"  20 :

print ( ’length should not be greater than 8’ )

val = False

  

if not any (char.isdigit () for char in passwd):

print ( ’Password should have at least one numeral’ )

  val = False

 

if not any (char.isupper () for char in passwd):

print ( ’Password should have at least one uppercase letter’ )

  val = False

 

if not any (char.islower () for char in passwd):

print ( ’Password should have at least one lowercase letter’ )

val = False

  

if not any (char in SpecialSym for char in passwd): 

print ( ’Password should have at least one of the symbols $ @ #’ )

val = False

if val:

return val

 
# Main method

def main ():

passwd = ’Geek12 @’

 

  if (password_check (passwd)):

print ( " Password is valid " )

else :

print ( "Invalid Password !!" )

 
Driver code

if __ name__ = = ’ __main__’ :

main ()

B Output:

 Password is valid 

This code used boolean functions to check if all conditions were met or not. We can see that although the complexity of the code is basic, the length is significant.

Method # 2: Using Regular Expressions

The compile () method of the Regex module creates a Regex object, which allows regex functions on the pat variable. We then check to see if the pattern defined by pat is accompanied by the input string passwd . If so, the search method returns true , which would allow the password to be valid.

# library import

import re

 

def main ():

  passwd = ’ Geek12 @ ’

reg = "^ (? =. * [az]) (? =. * [AZ]) (? =. * d) (? =. * [@ $!% * #? & amp;]) [A -Za-zd @ $! #% *? & Amp;] {6,20} $ "

  

# writing regular expressions

  pat = re. compile (reg)

 

# regular expression search

mat = re.search (pat, passwd)

 

# check conditions

if mat:

print ( "Password is valid." )

else :

  print ( "Password invalid !!" )

 
Driver code

if __ name__ = = ’ __main__’ :

main ()

Exit:

 Password is valid. 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically