Validation of a Password - Python
You can use re
module for regular expressions.
With it your code would look like this:
import re
def validate():
while True:
password = raw_input("Enter a password: ")
if len(password) < 8:
print("Make sure your password is at lest 8 letters")
elif re.search('[0-9]',password) is None:
print("Make sure your password has a number in it")
elif re.search('[A-Z]',password) is None:
print("Make sure your password has a capital letter in it")
else:
print("Your password seems fine")
break
validate()
Password Validation with for loop: Python
Try this:
import re
if re.search("[a-z]+", password) is None:
print('At least one character must be in the range [a-z]')
if re.search("[A-Z]+", password) is None:
print('At least one character must be in the range [A-Z]')
if re.search("[0-9]+", password) is None:
print('At least one character must be in the range [0-9]')
if re.search("[@#$]+", password) is None:
print('At least one character must be in the range [a@#$]')
password validation - python
I ran it and entered "test" and it said "Password is good". I suspect you meant to have:
password = input("Please enter password: ")
length = False
digit = False
capital = False
length = len(password)
if length > 6:
length = True
#print("Length Good")
for count in password:
if count.isdigit():
digit = True
#print ("Contains digit")
for count in password:
if count.isupper():
capital = True
#print ("Contains a capital")
if length == True and digit == True and capital == True:
print("Password is good")
else:
print("Bad password")
because otherwise you are enforcing bad passwords.
python password rules validation
The following should work, and removes the unnecessary imports :
def checkio(data):
return len(data) >= 10 and any(char.isdigit() for char in data) and any(char.islower() for char in data) and any(char.isupper() for char in data)
You can iterate over strings by default, and each string has the isdigit, islower, etc... methods that you can use. The any()
method returns True if any of the values returned by an iterable passed to it is true, and the something(value) for value in iterable
syntax creates a generator expression, which iterates over each character of the string and checks whether it's a digit/lowercase/uppercase character.
Based on this answer.
Benchmark time, with my horrible benchmark code (but it seems to do the job) :
from time import time
import re
data = "ULFFunH8ni" # the password
def benchmark(method): # benchmark method, loops 1000000 times and prints how much it took
start = time()
for _ in range(1000000): method(data)
print(time() - start)
def checkio_kasra(data): # Kasra's answer
return len(data) >= 10 and all([any(i.isdigit() for i in data),any(i.islower() for i in data),any(i.isupper() for i in data)])
def checkio_andreysabitov(data): # Andrey Sabitov's regex-based answer
if len(data) < 10:
return False
digital = re.compile('[0-9]+')
capital = re.compile('[A-Z]+')
lower = re.compile('[a-z]+')
return (digital.search(data) is not None) and (capital.search(data) is not None) and (lower.search(data) is not None)
def checkio_andredaniel(data): # My answer
return len(data) >= 10 and any(char.isdigit() for char in data) and any(char.islower() for char in data) and any(char.isupper() for char in data)
def checkio_shashank_bitmask(data):
if len(data) < 10: return False
lud_bitmask = 0
for ch in data:
if ch.islower():
lud_bitmask |= 4
if lud_bitmask == 7: return True
elif ch.isupper():
lud_bitmask |= 2
if lud_bitmask == 7: return True
elif ch.isdigit():
lud_bitmask |= 1
if lud_bitmask == 7: return True
return False
def checkio_shashank_pure_regex(data):
return bool(re.match(r'(?=.*?[0-9])(?=.*?[A-Z])(?=.*?[a-z]).{10}', data))
def checkio_shashank_impure_regex(data):
return len(data) >= 10 and re.match(r'(?=.*?[0-9])(?=.*?[A-Z])(?=.*?[a-z])', data)
benchmark(checkio_kasra)
benchmark(checkio_andreysabitov)
benchmark(checkio_andredaniel)
benchmark(checkio_shashank_bitmask)
benchmark(checkio_shashank_pure_regex)
benchmark(checkio_shashank_impure_regex)
The results, on my low-end tablet running Windows 7 and Python 3.4.x (ran it two times to be sure) :
$ python pass.py
6.333611011505127 # Shashank
9.625216960906982 # Kasra
11.450419902801514 # Andrey Sabitov
8.36161494255066 # Me
However, given a semi-incorrect input of 1XYZXYZXYZ
(length and digits are good, but all uppercase), some solutions fail to stop early :
7.456813097000122 # Shashank
9.328815937042236 # Kasra
11.169620037078857 # Andrey Sabitov
6.349210977554321 # Me
Note that these benchmarks don't take into account eventual edits. I suggest you run the benchmark on your own machine using the latest answers. Feel free to update this post with new results.
Related Topics
Can Multiprocessing Process Class Be Run from Idle
Comments Not Working in Jinja2
Why Can't I Change Only a Single Element in a Nested List in Python
Unicodeencodeerror: 'Ascii' Codec Can't Encode Character '\Xe9' - -When Using Urlib.Request Python3
Keep Persistent Variables in Memory Between Runs of Python Script
Using a Where _ in _ Statement
How to Add an Empty Column to a Dataframe
Trouble Installing Textblob with Pip
How to Add a Question Mark [] Button on the Top of a Tkinter Window
Python: How to Run Eval() in the Local Scope of a Function
My Py2App App Will Not Open. What's the Problem
Pip Broke. How to Fix Distributionnotfound Error
How to Plot and Annotate Grouped Bars in Seaborn/Matplotlib
Replace Column Values Based on Another Dataframe Python Pandas - Better Way
Python Pyqt Signals Are Not Always Working
Anyone Know of a Good Python Based Web Crawler That I Could Use