14
u/csabinho May 05 '26
Take a look into regular expressions. That's how you can do such a checker properly.
6
u/autoglitch May 05 '26 edited May 05 '26
I second this. Regex will turn this into nearly one line and it's very fast. It's also very useful for string parsing in the future.
Although.. regex is harder to update as the rules change. Your method is good for learning but in practice I'd use a regex and just put the rules it checks in comments.
3
u/csabinho May 05 '26
Commenting regexes is an important recommendation. And actually an important rule everyone should follow, as long as those aren't trivial.
4
u/Rhylanor-Downport May 05 '26
Agree. If you have a problem that requires a regex you now have two problems.
1
u/Creative-Category344 May 07 '26
Regex is solid, but I would also look at the
stringmodule for checking character types since it reads cleaner than escape sequences.
11
4
4
u/autoglitch May 05 '26
You can simplify your if statements with 'any'.
char_list = ['a', 'b', 'c']
text = "hello world"
if any(char in text for char in char_list):
print("Match found!")3
u/PayAffectionate2300 May 05 '26
Theres quite a bit you can improve on but if you beginner and it’s your first “project attempt” it’s not bad. Try to think about what happens if I don’t have ABC but have “D”. You can’t write “or” for each alphabet (technically you can but) try Googling for a more efficient way of testing everything at once. Same with the numbers
2
2
u/avataru May 06 '26
While regex is the way to go for such checks, even with multi line and neatly commended, do non try to cram everything into a single rule/check because it will become unmanageable.
You might also look into multi-byte characters such as emojis. They are not "special characters" in the sense most strength checkers would accept them, and they can break your length checks, but sure are valid characters. Adding such a check in the same regex will really complicate things.
1
u/Due_Scholar9732 May 06 '26
I wouldn't say I know much about python but aren't you only including A,B,C, not A=>Z? Does that work automatically?
1
1
u/cjneutron May 06 '26
Regex will be faster if the character conditions are met towards the end of the password string and the bit flag loop will be faster if the conditions are met towards the beginning. So regex would probably win in the real world since a lot of people will add the needed special characters at the end once they try a password and they don't have the correct characters.
import re
PASSWORD_RE = re.compile(r"^(?=.*[A-Z])(?=.*\d)(?=.*[@#$]).{6,}$")
def is_strong_password(password: str) -> bool:
return bool(PASSWORD_RE.match(password)). .
def is_strong_password(password: str, min_length: int = 6) -> bool:
if len(password) < min_length:
return False
flags = 0
for ch in password:
o = ord(ch)
if 65 <= o <= 90:
flags |= 1
elif 48 <= o <= 57:
flags |= 2
elif ch in "@#$":
flags |= 4
if flags == 7:
return True
return False1
1
May 06 '26
[removed] — view removed comment
1
u/Majestic_Arachnid132 May 06 '26
sorry about that, I did try to create my own password checker but specifically to understand functions and return values since I'm only at the basic and the only project I did other than that is a calculator using loops.
19
u/riklaunim May 05 '26
If/Else statements that return True/False can be simplify to return the condition itself :)
You can also use .isalnum() for example. You could also google around for "common passwords" or a dictionary of popular words and check if any is the password and mark it as weak.