Search code examples
pythonstringuppercaselowercase

Does python see an empty string as uppercase or lowercase?


It's embarrassing, that we have quite a few questions with all the following tags: , but no question asking about whether python sees the empty string as uppercase or lowercase. So, does python see the empty string as uppercase or lowercase?


Solution

  • If you run this:

    empty = ""
    
    if empty.islower():
        print("The empty string is lowercase")
    elif empty.isupper():
        print("The empty string is uppercase")
    else:
        print("The empty string is neither!")
    

    You should get:

    The empty string is neither!
    

    This is because isupper first checks if there is more than one character before proceeding on to checking its case (look here):

    Python isupper is one of the Python String Method used to check whether the given string has at least one character, and the character is either in uppercase or not. If it is in Uppercase, then the Python isupper function returns True; otherwise, it returns False.