In Python, we can check if a character is uppercase by checking if the letter is equal to the letter after applying the Python upper() function.
def checkCharUpper(x):
return x == x.upper()
print(checkCharUpper("a"))
print(checkCharUpper("A"))
#Output:
False
True
When processing strings in a program, it can be useful to know which letters are uppercase and which characters are lowercase. Using Python, we can easily check if a character is uppercase with the help of the Python upper() function.
To check if a letter is uppercase, we just need to check if that letter is equal to that letter after applying the upper() function.
Below is a Python function which will check if a character is uppercase.
def checkCharUpper(x):
return x == x.upper()
print(checkCharUpper("a"))
print(checkCharUpper("A"))
#Output:
False
True
How to Check if a Letter is Lowercase in Python
We can also check if a character is lowercase in Python very easily.
To check if a letter is lowercase in Python, we can adjust our function that we defined above to use the Python lower() function instead of the upper() function.
Below is a Python function which will check if a character is lowercase.
def checkCharLower(x):
return x == x.lower()
print(checkCharLower("a"))
print(checkCharLower("A"))
#Output:
True
False
Hopefully this article has been useful for you to learn how to check if a character is uppercase in Python.