Search code examples
pythonuppercaselowercase

Check if all letter is uppercase or lowercase python, if there is one word in string is different print none


For input string S, print UPPER if string S contains only uppercase characters (may contain spaces), LOWER if string S contains only lowercase characters (may contain spaces), otherwise print out NONE.

so here is my code

test_str = input()

  
res = "LOWER"
for ele in test_str:
  
    # checking for uppercase character and flagging
    if ele.isupper():
        res = "UPPER"
        break

print(str(res))

But if a word is neither lowercase nor uppercase, it doesn't None. How can I solve this?


Solution

  • No need for loops here. Just simply use the functions isupper() and islower() in an if-elif block, and add an else block to take care of mixed case (i.e., print out None or NONE), like so:

    test_str = input()
        
    if test_str.isupper():
        print('UPPER')
    elif test_str.islower():
        print('LOWER')
    else:
        print(None) # or print('NONE')
    

    Example input/output:

    HELLO USER
    UPPER
    
    HELLO UsER    
    None 
    
    hello user  
    LOWER
    
    hello User
    None 
    

    Active reading: GeeksForGeeks