Python 程序来检查密码的有效性?
这里给定了一个密码,我们的任务是检查此密码是否有效。这里我们使用 re 模块提供正则表达式,re.search() 用于检查字母、数字或特殊字符的有效性。
算法
Step 1: first we take an alphanumeric string as a password. Step 2: first check that this string should minimum 8 characters. Step 3: the alphabets must be between a-z. Step 4: At least one alphabet should be in Uppercase A-Z. Step 5: At least 1 number or digit between 0-9. Step 6: At least 1 character from [_ or @ or $].
示例代码
# Python program to check valid password import re passw = input("Enter Password ::>") fl = 0 while True: if (len(passw)<8): fl= -1 break elif not re.search("[a-z]", passw): fl = -1 break elif not re.search("[A-Z]", passw): fl = -1 break elif not re.search("[0-9]", passw): fl = -1 break elif not re.search("[_@$]", passw): fl = -1 break elif re.search("\s", passw): fl = -1 break else: fl = 0 print(" This Is Valid Password") break if fl ==-1: print("Not a Valid Password")
输出
Enter Password ::> vbnA@hj9 This Is Valid Password
广告