The Python Oracle

I have to check if the string contains: alphanumeric, alphabetical , digits, lowercase and uppercase characters

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Dreaming in Puzzles

--

Chapters
00:00 I Have To Check If The String Contains: Alphanumeric, Alphabetical , Digits, Lowercase And Uppercase
00:23 Answer 1 Score 0
01:01 Accepted Answer Score 8
01:24 Answer 3 Score 2
01:32 Answer 4 Score 0
01:40 Thank you

--

Full question
https://stackoverflow.com/questions/5132...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #string #python3x

#avk47



ACCEPTED ANSWER

Score 8


If you want to check if the whole string contains those different characters types then you don't actually have to loop through the string. You just can use the any keyword.

def fun(s):
    if any(letter.isalnum() for letter in s):
        print("Is alphanumeric")
    if any(letter.isalpha() for letter in s):
        print("Is alpha")
    if any(letter.isdigit() for letter in s):
        print("Is digit")
    if any(letter.isupper() for letter in s):
        print("Is upper")
    if any(letter.islower() for letter in s):
        print("Is lower")

s=str(input())
fun(s)



ANSWER 2

Score 2


string = input()

print (any(c.isalnum() for c in s))
print (any(c.isalpha() for c in s))
print (any(c.isdigit() for c in s))
print (any(c.islower() for c in s))
print (any(c.isupper() for c in s))



ANSWER 3

Score 0


You are applying string methods to entire words rather than to individual characters. You are not applying an any or all condition to check whether any or all of the characters in each word fulfill each criterion.

For example, with an any condition for each word:

def fun(s):
    for i in s:
        print('\n', i)
        if any(letter.isalnum() for letter in i):
            print('Alnum', True)
        if any(letter.isalpha() for letter in i):
            print('Alpha', True)
        if any(letter.isdigit() for letter in i):
            print('Digit', True)
        if any(letter.isupper() for letter in i):
            print('Upper', True) 
        if any(letter.islower() for letter in i):
            print('Lower', True)

s = input().split()
fun(s)

Example result:

One test 123

 One
Alnum True
Alpha True
Upper True
Lower True

 test
Alnum True
Alpha True
Lower True

 123
Alnum True
Digit True



ANSWER 4

Score 0


if __name__ == '__main__':
    s = input()
    print (any([c.isalnum() for c in s]))
    print (any([c.isalpha() for c in s]))
    print (any([c.isdigit() for c in s]))
    print (any([c.islower() for c in s]))
    print (any([c.isupper() for c in s]))