If statement to check whether a string has a capital letter, a lower case letter and a number

if (any(x.isupper() for x in s) and any(x.islower() for x in s) 
    and any(x.isdigit() for x in s) and len(s) >= 7):

Another way is to express your rules as a list of (lambda) functions

rules = [lambda s: any(x.isupper() for x in s), # must have at least one uppercase
        lambda s: any(x.islower() for x in s),  # must have at least one lowercase
        lambda s: any(x.isdigit() for x in s),  # must have at least one digit
        lambda s: len(s) >= 7                   # must be at least 7 characters
        ]

if all(rule(s) for rule in rules):
    ...

Regarding your comment. To build an error message

errors = []
if not any(x.isupper() for x in password):
    errors.append('Your password needs at least 1 capital.')
if not any(x.islower() for x in password):
    errors.append(...)
...

if errors:
    print " ".join(errors)

import re

s   = 'fooBar3'
rgx = re.compile(r'\d.*?[A-Z].*?[a-z]')

if rgx.match(''.join(sorted(s))) and len(s) >= 7:
    print 'ok'

Even more fun is this regex, which will report the type of character that is missing:

s = 'fooBar'

rules = [
    r'(?P<digit>\d)?',
    r'(?P<upper>[A-Z])?',
    r'(?P<lower>[a-z])?',
]

rgx      = re.compile(r'.*?'.join(rules))
checks   = rgx.match(''.join(sorted(s))).groupdict()
problems = [k for k,v in checks.iteritems() if v is None]

print checks   # {'upper': 'B', 'digit': None, 'lower': 'a'}
print problems # ['digit']

Finally, here's a variant of the excellent rules-based approach suggested by gnibbler.

s = 'fooBar'

rules = [
    lambda s: any(x.isupper() for x in s) or 'upper',
    lambda s: any(x.islower() for x in s) or 'lower',
    lambda s: any(x.isdigit() for x in s) or 'digit',
    lambda s: len(s) >= 7                 or 'length',
]

problems = [p for p in [r(s) for r in rules] if p != True]

print problems  # ['digit', 'length']

Tags:

Python