Really, I can have any() thing?!?

So, I’ve been using HackerRank, working my way up (at this writing, I’ve got 205 points in Python [309 Hackos points] and I’m ranked 10505), and one of the python items I just finished doing is on string validators…

These let you test if a string is just alpha, alphanumeric, just digits, is upper or is lower case…

But I digress…

The point of the exercise was given a string, determine if any of it was each of these (alpha, alphanumeric, digit, upper or lower), and print if each was true or not.

This is where, looking around online, I discovered the python any() command…

I was able to take this:

results = [False, False, False, False, False]
S = input().strip()
for c in S:
    if c.isalnum():
        results[0] = True
    if c.isalpha():
        results[1] = True
    if c.isdigit():
        results[2] = True
    if c.islower():
        results[3] = True
    if c.isupper():
        results[4] = True
print(*results, sep='\n')

which I thought was a decent solution prior to my discovery, into this:

S = input().strip()
print(any(s.isalnum() for s in S))
print(any(s.isalpha() for s in S))
print(any(s.isdigit() for s in S))
print(any(s.islower() for s in S))
print(any(s.isupper() for s in S))

which I thought was pretty useful… I’m liking python more and more every day I use it!

Anyway You Want It – Journey
Never B Flat, Sometimes B Sharp, Always B Natural