How to check if lowercase letters exist?

I observe unusual behavior with the methods islower()and .isupper()in Python. For instance:

>>> test = '8765iouy9987OIUY'
>>> test.islower()
False
>>> test.isupper()
False

However, the following mixed string value seems to work:

>>> test2 = 'b1'
>>> test2.islower()
True
>>> test2.isupper()
False

I do not understand this anomaly. How to define lowercase letters as in test?

+4
source share
5 answers

islower()and isupper()return only Trueif all letters in the string are lowercase or uppercase, respectively.

You will need to check individual characters; any()and the generator expression makes it relatively efficient:

>>> test = '8765iouy9987OIUY'
>>> any(c.islower() for c in test)
True
>>> any(c.isupper() for c in test)
True
+11
source

From the documentation :

ISLOWER()      true, , false.

ISUPPER()      true, , false.

, "1".islower() "1".isupper() False. , false.

, :

>>> test = '8765iouy9987OIUY'
>>> "".join([i for i in test if not i.islower()])
'87659987OIUY'
+6

re :

import re
print re.findall(r"[a-z]",'8765iouy9987OIUY')

:

['i', 'o', 'u', 'y']

, [] . a z.

+5

map():

map(str.isupper, '8765iouy9987OIUY')
# output: [False, False, False, False, False, False, False, False,
#         False, False, False, False, True, True, True, True]

any() :

any(map(str.isupper, '8765iouy9987OIUY'))
# output: True
+3

For a password policy that requires at least 1 character, upper, lower, and digital. I think this is the most readable and easiest approach.

def password_gen():
while True:
    password = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits) for n in range(12))
    tests = {}
    tests['upper'] = any(char.isupper() for char in password)
    tests['lower'] = any(char.islower() for char in password)
    tests['digit'] = any(char.isdigit() for char in password)
    if all(tests.values()):
        break
return password
0
source

Source: https://habr.com/ru/post/1535989/


All Articles