Check if a character is uppercase in Python

avatar

Borislav Hadzhiev

Last updated: Sep 18, 2022

banner

Photo from Unsplash

Check if a character is uppercase in Python #

Use the str.isupper() method to check if a character is uppercase, e.g. if char.isupper():. The str.isupper() method will return True if the character is uppercase and the string contains at least one cased character, otherwise False is returned.

main.py
char = 'B' if char.isupper(): # 👇️ this runs print('The character is uppercase') else: print('The character is NOT uppercase') print('B'.isupper()) # 👉️ True print('b'.isupper()) # 👉️ False print('-'.isupper()) # 👉️ False print(' '.isupper()) # 👉️ False print(''.isupper()) # 👉️ False

We used the str.isupper() method to check if a character is uppercase.

The str.isupper method returns True if all cased characters in the string are uppercase and the string contains at least one cased character, otherwise False is returned.

main.py
my_str = 'BOBBYHADZ.COM' all_uppercase = my_str.isupper() print(all_uppercase) # 👉️ True

If you need to check if a character is an ASCII uppercase letter, use the in operator.

main.py
import string print(string.ascii_uppercase) # 👉️ ABCDEFGHIJKLMNOPQRSTUVWXYZ print('B' in string.ascii_uppercase) # 👉️ True print('Б' in string.ascii_uppercase) # 👉️ False

We used the string.ascii_uppercase attribute to get a string containing the uppercase letters from A to Z.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

If you need to check if the first letter in a string is uppercase, access it at index 0.

main.py
my_str = 'Bobby' if my_str[0].isupper(): print('First letter is uppercase') print(my_str[0].isupper()) # 👉️ True print(my_str[1].isupper()) # 👉️ False

If you need to check if all words in a string start with an uppercase letter, use the all() function.

main.py
my_str = 'Name: Borislav Hadzhiev' result = all(word[0].isupper() for word in my_str.split()) print(result) # 👉️ True

We used the str.split() method to split the string into a list of words and used the str.isupper() method to check if the first letter in each word is uppercase.

The all() built-in function takes an iterable as an argument and returns True if all elements in the iterable are truthy (or the iterable is empty).

You can use the str.join() method if you need to extract the uppercase characters from a string.

main.py
my_str = 'BOBBYhadz123' only_upper = ''.join(char for char in my_str if char.isupper()) print(only_upper) # 👉️ BOBBY only_upper = [char for char in my_str if char.isupper()] print(only_upper) # 👉️ ['B', 'O', 'B', 'B', 'Y']

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

The second example uses a list comprehension to extract the uppercase letters into a list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.