Password validation in Python

29 Dec, 2020
Let’s take a password as a combination of alphanumeric characters along with special characters, and check whether the password is valid or not with the help of few conditions.
Conditions for a valid password are:
Should have at least one number.Should have at least one uppercase and one lowercase character.Should have at least one special symbol.Should be between 6 to 20 characters long.
Should have at least one number.
Should have at least one uppercase and one lowercase character.
Should have at least one special symbol.
Should be between 6 to 20 characters long.
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
We can check if a given string is eligible to be a password or not using multiple ways.
Method #1: Naive Method (Without using Regex).
# Password validation in Python# using naive method  # Function to validate the passworddef password_check(passwd):          SpecialSym =['$', '@', '#', '%']    val = True          if len(passwd) < 6:        print('length should be at least 6')        val = False              if len(passwd) > 20:        print('length should be 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 methoddef main():    passwd = 'Geek12@'          if (password_check(passwd)):        print("Password is valid")    else:        print("Invalid Password !!")          # Driver Code        if __name__ == '__main__':    main()
Password is valid

This code used boolean functions to check if all the conditions were satisfied or not. We see that though the complexity of the code is basic, the length is considerable. Method #2: Using regex
compile() method of Regex module makes a Regex object, making it possible to execute regex functions onto the pat variable. Then we check if the pattern defined by pat is followed by the input string passwd. If so, the search method returns true, which would allow the password to be valid.
# importing re libraryimport re  def main():    passwd = 'Geek12@'    reg = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!#%*?&]{6,20}$"          # compiling regex    pat = re.compile(reg)          # searching regex                     mat = re.search(pat, passwd)          # validating conditions    if mat:        print("Password is valid.")    else:        print("Password invalid !!")  # Driver Code     if __name__ == '__main__':    main()
Password is valid.

Python Regex-programs
python-regex
python-string
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Python map() function
Adding new column to existing DataFrame in Pandas
How to get column names in Pandas dataframe
Different ways to create Pandas Dataframe
Python program to convert a list to string
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary
Python Program for Fibonacci numbers