I have made a password verification "program" which takes user input and checks whether the password is valid or not. Based on that, a reply is printed.

print("Create a password! Your password must have 8 to 12 digits. Numbers and lower as well as upper case letters must be a part of it!")
res = any(chr.isdigit() for chr in password)
res2 = any(chr.islower() for chr in password)
res3 = any(chr.isupper() for chr in password)
if len(password) >= 8 and len(password) <13 and res == True and res2 == True and res3 == True :
print("Welcome!")
else:


Is there a more elegant or better way? Maybe you also have some ideas on how to expand the project?

• Don't worry, I was not planing on using this for anything. It was just a small project so that I have something specific to look into. Otherwise getting into programming seems pretty overwhelming – morloq Mar 23 at 18:28
• I remember that overwhelming feeling (hell, I still have it sometimes), but don‘t let it discourage you. For me the key was to find projects that steadily increase in complexity. The closer a project is to a real use case, the better. – riskypenguin Mar 25 at 15:38

1. Variable naming: res, res2, res3 are not descriptive, so it's harder to understand at first glance what their purpose is. Instead I would recommend something similiar to contains_digit, contains_lower_char, contains_upper_char. Variable naming is always up to a bit of personal preference, but explicit names are generally preferable.

2. Conditionals: The way the condition in your if-statement is set up is suboptimal. Firstly: len(password) >= 8 and len(password) <13 can be shortened to 8 <= len(password) <= 12 or len(password) in range(8, 13). This is more readable and depicts your intentions in a more concise way. Secondly: You almost never need == True clauses since True == True -> True and False == True -> False. So the second part of your condition can be shortened to res and res2 and res3. This is also where better variable names make the functionality of your program way clearer.

To avoid multiple concatenated and-statements you could probably use something like all([len(password) in range(8, 13), res, res2, res3]), but I find this usually decreases readbility.

To conclude I would suggest the following if-condition:

if 8 <= len(password) <= 12 and contains_digit and contains_lower and contains_upper:


On a side note: This is not a password generator, but a password validity checker. You might want to additionally check, that the password only includes ASCII-characters if that is relevant.

• Thank you! That's really helpful, I can see what you mean – morloq Mar 21 at 17:03
• Glad to help. Please consider marking the question as answered (i.e. accept an answer) if you requirements are met, so other contributors know the question does not require further attention. – riskypenguin Mar 21 at 17:05
• Please consider upvoting the question as well. If it was worth answering, it should've been worth a vote too. Otherwise, why did you answer it? ;-) – Mast Mar 21 at 17:17
• @mast is that a thing? – theonlygusti Mar 24 at 16:33

Not much more to add here, but you'll quickly find that this routine is too basic for the need and regular expressions are the way to go.

This routine does not guarantee that the resulting password will be balanced, that is sufficiently "random" and hard to guess. For example AAAbbb123 or Abcdef123 will pass your test. These are not strong passwords and they may even be in some lists of common passwords (pwnlists), which means they are less likely to withstand brute force attempts. Or what about Ab1 followed by 5 or more whitespace characters ? The point is that really poor patterns will get through.

On the other hand, a really good password is hard to remember for humans but that's why we have password managers, especially that each password should be unique and not reused across sites.

@riskypenguin suggests that you may want to restrict input to ASCII characters only. I'm not sure I would do that. Using non-standard strings increases complexity. You may have foreign users who are accustomed to their native script, for example Japanese. They may want to use their keyboard in "native mode" without switching to ASCII.

Due to the complexity, many developers prefer to ignore the subject and stick to ASCII but see below if you want to learn more.

Nowadays websites usually use the UTF-8 character set and this usually holds true for database storage. Plus, you are not supposed to store the plaintext password but a hash, like a salted SHA-512 hash, which is plain ASCII.

Finally, limiting the length to 12 characters is a poor choice. Some users may want to use longer passwords, or a more memorable passphrase. The choice of 12 characters is arbitrary and puts a cap on complexity. The real limit should be the length of the password field in your HTML forms. Yeah, 50 characters should be reasonable maybe. But 12 ? This is so 1996 I think :)

References:

• The main and super important point here is that the actual policy being implemented by OP is bad, and has been known to be bad for many years. I pity the users if this code ever makes it into production. – l0b0 Mar 21 at 23:36
• A really good password is hard to remember for humans? Hardly, as you seem to allude to later. See security.stackexchange.com/questions/6095/… Of course, there are systems imposing unreasonable restrictions regarding password-length and contents to work in an insecure manner. – Deduplicator Mar 22 at 1:25
• "Finally, limiting the length to 12 characters is a poor choice." +1 for that alone. – infinitezero Mar 22 at 8:46
• store the plaintext password but a hash, like a salted SHA-512 hash no, you should not do that.SHA is a fast hash and you do not want that. You want it to be slow, independent on hardware etc. There are special hashes done just for that (PBKDF2, bcrypt, argon ...) – WoJ Mar 22 at 14:22

There are two separate questions here, and I'll try to tackle both. You're asking how to improve the code, which I'll separate into two halves: "How can I make this into better code that does the same thing?", and "How can I make this do better things?".

Making the code better while doing the same thing:

if len(password) >= 8 and len(password) <13 and res == True and res2 == True and res3 == True :


You're doing a "betweenness" check here, which Python actually makes really easy. Also, you don't have to check if something is equal to True - just check if it is. So, try this:

if 8 <= len(password) < 13 and res and res2 and res3:


Others have mentioned variable naming, so I'll just briefly say that I agree, and "has_lower" and so on would be better than "res2".

It's a very small UI adjustment, but I prefer a space after a colon prompt.

password = input("Enter your password:")


So, on to doing things in better ways.

An easy one: Check out the getpass module for a better way to ask the user for a password. Where possible, it'll mask the password, or hide it from history, or whatever else is appropriate for the environment it's in.

Secondly: Don't ever limit the length of a password, other than to cut off really ridiculous things that wouldn't actually be passwords at all. For instance, a limit of 50 (as has been suggested) is probably not a problem, but I'd go all the way to 256 or more, unless you have something that actually can't work with passwords that long. There's definitely no reason to cap it at thirteen.

If you want to get really sophisticated, what you can consider is a hybrid of bank-style password requirements ("must have at least one upper, one lower, one digit, one symbol, and one character that can't be typed on a US English keyboard") and a much more secure length requirement. Permit XKCD 936 style passwords by simply allowing any password greater than some length requirement. Since this is an exercise in Python coding, here's what I'd recommend: Give a score to each category of character you've found (eg 26 points for a lower-case letter, 26 more for an upper-case, 10 for a digit, etc), add up all those scores, and raise it to the power of the length of the password. That's a decent way to evaluate a password's complexity, although it's never going to be perfect (XKCD 936 passwords are extremely difficult to judge fairly unless you know the original word list); you can then play around with it and see just how long a pure-lower-case-ASCII-letter password needs to be in order to score as well as a more bank-complex password. HINT: It's not nearly as long as many people's intuition says it is.

If you want to actually get into password generation, as opposed to validation, I'd recommend looking at the secrets module, which has a variety of tools that will help you.

Have fun with it!

• unless you have something that actually can't work with passwords that long this usually means bad news. When a password length is capped, I am always extremely suspicious of the way it is handled. Having a 1MB big password should not be an issue. With 1GB you may run into some timeout and performance issues, but never security ones. (examples of 1 MB or 1GB passwords are of course exaggerated) – WoJ Mar 22 at 14:25
• Yep. Depending on the protocol, sometimes it's worth setting a practical limit, just to prevent protocol failures, and there's always the possibility that there's something in the system that doesn't want a password more than (say) 65536 bytes. I'm not ruling out the possibility. But yeah, you definitely don't want to cap it at 13 characters. – rosuav Mar 23 at 15:03
• there's always the possibility that there's something in the system that doesn't want a password more than (say) 65536 bytes → that should never be the case. The password is checked against a hash (so it does not matter if the password is 1 or 98098080809 chars long), and then there is a session token (or similar) that is being used for authorization (and relayed further on). If there is a limit because "something does not play well with the length" then it is a real security issue. – WoJ Mar 23 at 15:49
• (cont'd) If there is capping for performance reasons then fine (but it will be very large anyway). A browser will set a (huge) limit itself, but having 256 (MS choice) or 10000 should not be an issue. – WoJ Mar 23 at 15:49
• Right, that's what I'm saying - it would be a ridiculously high limit. Some protocols simply don't play well with stupid-large data; for instance, if you're sending information over UDP, you really want to keep it within one packet if you can. (Though if you're sending passwords in clear text over UDP, you have bigger problems.) Any length limit on the password needs to be significantly greater than the hash length, for a start. – rosuav Mar 24 at 6:57

I'm not going to retype what everyone already said, but I'll add a quick suggestion.

Use Pylint to help you write better code. It helped me tremendously when starting. As an example, Pylint would mark the following in your code.

On the first line: Line too long (141/100)
On each res == true (etc..): Comparison 'res == True' should be 'res is True' if checking for the singleton value True, or 'bool(res)' if testing for truthiness.


And just because I'm in the mood of giving suggestions, take a look at venv.

• venv is irrelevant here, no point burdening a new(ish) programmer with too much hassle :) Incidentally, bool(res) is a poor choice too - just res is the best way here. Not sure why pylint recommended that. – rosuav Mar 24 at 6:58
• PEP 8 says "Don’t compare boolean values to True or False using ==:". I would have expected Pylint to pick that up. What Pylint warnings have you disabled and what version of Pylint (pylint --version)? – Peter Mortensen Mar 24 at 15:15
• I use Pylint 2.6.0 (Ubuntu MATE 20.04 (Focal Fossa)), and I get C0121: Comparison to True should be just 'expr' (singleton-comparison). – Peter Mortensen Mar 24 at 15:17
• This confusing Pylint error message seems to have been fixed at the end of 2018. – Peter Mortensen Mar 24 at 15:50
• On newer versions of Debian/Ubuntu, installing Pylint with pip install pylint will not work. Instead, use pip3 install pylint. – Peter Mortensen Mar 24 at 16:04

Welcome to the wonderful world of programming!

While writing code it is helpful to think of similar problems you can solve. Not sure if you are specifically looking for a password verification routine (which you should use an established library instead of rolling your own), or if you are just going through an academic exercise.

Exercises are good to help learn a language or explore a problem, even if they are never truly production worthy. Here is your password routine re-envisioned as a Product Number verifier. Same basic problem of testing contents of Strings, but removes the whole password discussion.

print( "Enter product number: normally 8 to 12 characters with numerics and upper and lower case letters..." )
allegedProduct = input( "Product: " )

# control checks
isValid = True # optimistic

if( not any( chr.isdigit() for chr in allegedProduct ) ) :
isValid = False

if( not any( chr.islower() for chr in allegedProduct ) ) :
isValid = False

if( not any( chr.isupper() for chr in allegedProduct ) ) :
isValid = False

if( not ( 8 <= len( allegedProduct ) <= 12 ) ) :
isValid = False

if( isValid ) :
print( "Product Number format confirmed." )
else:
print( "Incorrect Product Number format." )


Of course, the If statements can be combined as follows...

print( "Enter product number: normally 8 to 12 characters with numerics and upper and lower case letters..." )
allegedProduct = input( "Product: " )

# control checks
if( ( not any( chr.isdigit() for chr in allegedProduct ) ) or
( not any( chr.islower() for chr in allegedProduct ) ) or
( not any( chr.isupper() for chr in allegedProduct ) ) or
( not ( 8 <= len( allegedProduct ) <= 12 ) ) ) :
print( "Incorrect Product Number format." )
else:
print( "Product Number format confirmed." )


Keep testing. Keep iterating. And keep playing. Have fun!

• Welcome to the Code Review Community. The point of the code review community is to help the original poster improve their coding skills so answers should make meaningful observations about the code in the question. This answer doesn't seem to address the code in the question at all. – pacmaninbw Mar 24 at 11:54

I don't have any Python implementation feedback, but I do have a little bit of security design feedback.

When you ask the user to specify an initial password, you should not echo the output to the screen (in case an interloper is watching them).

When you ask the user to specify an initial password, you should ask them to enter it twice, and then check additionally that the two passwords are the same. This helps the user avoid mistyping when the output is not echoed.

It is not a good design to provide a password length upper limit (You specified an arbitrary upper limit of 13). Because "George Bush speaks Spanish." is quite a complex password that is easy to remember and type correctly. The technique is known as a "pass phrase".

The terminology for the character classes you asked for are "character groups". Systems designed to be highly secure often ask for three or four of the following four character groups:

• lowercase ASCII letters

• uppercase ASCII letters

• digits

• punctuation

BUT: some of the best and most recent advice from the USA National Institutes of Standards and Technology specify that users should just be able to choose their password, not to impose any composition rules (i.e. character group requirements) on them, and instead to check their proposal against a list of known bad or previously breached passwords. Such a list is available from the haveibeenpwned.com service.

In addition to ASCII characters, you might have a thought about whether users might like to use characters not found in ASCII. As long as you think the user would find it useful, I feel that any typeable and printable character could be allowed in a password. The limit is the range of systems a user might have to use, and the limitations of those input methods. That is a justification for restricting to ASCII.