Table of Contents
Python Exercise 17 Problem:
In this Python exercise, write a Python program that will prompt a user to input a password. The password must be validated to meet a certain criteria as shown below with regular expressions (RegEx).
Exercise Requirements:
- Minimum length of transaction password: 8
- Maximum length of transaction password: 15
- Minimum of 1 letter between: a-z
- Minimum of 1 letter between: A-Z
- Minimum of 1 number: 0-9
- Minimum of 1 character: !@#$%^&*
Solution Python Exercise 17
Python Code Input:
import re symbols = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')'] print("""The password must meet the below requirements: Minimum of 8 characters Maximum of 15 characters Must have lowercase alphabetical characters: a-z Must have 1 alphabetical character should be of Upper Case: A-Z Must have 1 number between 0-9 Must have 1 special character from !@#$%^&* """) password = input("Please enter a password: ") def regexPasswordValidation(): regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*])[A-Za-z\d@$!#%*?&]{8,15}$" regex_compile = re.compile(regex) valid = re.search(regex_compile, password) if valid: print("Valid Password") else: print("Invalid Password") regexPasswordValidation()
User Input for Valid Password:
Please enter a password: ABH12Ab$
Output for Valid Password:
Valid Password
User Input for Invalid Password:
Please enter a password: an49K
Output for Invalid Password:
Invalid Password