Check if all characters in a String are Uppercase in Python

avatar

Borislav Hadzhiev

Last updated: Sep 18, 2022

banner

Photo from Unsplash

Check if all characters in a String are Uppercase in Python #

Use the str.isupper() method to check if all characters in a string are uppercase, e.g. if my_str.isupper():. The str.isupper() method returns True if all characters in the string are uppercase and the string contains at least one cased character.

main.py
import re my_str = 'BOBBYHADZ.COM' # โœ… check if all characters in a string are uppercase all_uppercase = my_str.isupper() print(all_uppercase) # ๐Ÿ‘‰๏ธ True # ---------------------------------------------------------- # โœ… check if all characters are uppercase and string contains only letters all_uppercase = my_str.isupper() and my_str.isalpha() print(all_uppercase) # ๐Ÿ‘‰๏ธ False # ---------------------------------------------------------- # โœ… check if string contains only ASCII uppercase letters match = re.match(r'^[A-Z]+$', 'BOBBY') print(match) # ๐Ÿ‘‰๏ธ <re.Match object; span=(0, 5), match='BOBBY'>

The first example uses the str.upper() method to check if all characters in a string are uppercase.

main.py
my_str = 'BOBBYHADZ.COM' all_uppercase = my_str.isupper() print(all_uppercase) # ๐Ÿ‘‰๏ธ True if all_uppercase: # ๐Ÿ‘‡๏ธ this runs print('All characters in the string are uppercase') else: print('NOT all characters in the string are 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
print(''.isupper()) # ๐Ÿ‘‰๏ธ False print('Bobby'.isupper()) # ๐Ÿ‘‰๏ธ False print('BOBBY'.isupper()) # ๐Ÿ‘‰๏ธ True print('B_O_B_B_Y'.isupper()) # ๐Ÿ‘‰๏ธ True

The str.isupper() method doesn't check if the string contains only letters.

Check if all characters in a String are Uppercase letters #

To check if all characters in a string are uppercase letters:

  1. Use the str.isupper() method to check if all characters in the string are uppercase.
  2. Use the str.isalpha() method to check if the string contains only letters.
  3. If both conditions are met, all characters in the string are uppercase letters.
main.py
my_str = 'BOBBYHADZ.COM' all_uppercase = my_str.isupper() and my_str.isalpha() print(all_uppercase) # ๐Ÿ‘‰๏ธ False if all_uppercase: print('All characters are uppercase and string contains only letters') else: # ๐Ÿ‘‡๏ธ this runs print('Not all characters are uppercase or string contains non-letters')

We used the boolean and operator, so for the variable to store a True value, both conditions have to be met.

The str.isalpha() method returns True if all characters in the string are alphabetic and there is at least one character, otherwise False is returned.

main.py
print('BOBBY'.isalpha()) # ๐Ÿ‘‰๏ธ True # ๐Ÿ‘‡๏ธ contains space print('BOBBY HADZ'.isalpha()) # ๐Ÿ‘‰๏ธ False
The str.isalpha method considers alphabetic characters the ones that are defined in the Unicode character database as "Letter".

The str.isalpha() method also returns True for non-ASCII characters.

main.py
my_str = 'ะคะคะค' all_uppercase = my_str.isupper() and my_str.isalpha() print(all_uppercase) # ๐Ÿ‘‰๏ธ True

If you need to check if all characters in the string are uppercase ASCII letters, use the re.match() method.

Check if all characters in a String are ASCII Uppercase letters #

Use the re.match() method to check if all characters in a string are ASCII uppercase letters, e.g. re.match(r'^[A-Z]+$', 'BOBBY'). The re.match() method will return a match object if all characters in the string are ASCII uppercase letters, otherwise None is returned.

main.py
import re match = re.match(r'^[A-Z]+$', 'BOBBY') print(match) # ๐Ÿ‘‰๏ธ <re.Match object; span=(0, 5), match='BOBBY'> if match: # ๐Ÿ‘‡๏ธ this runs print('All characters in the string are uppercase ASCII letters') else: print('Not all characters in the string are uppercase ASCII letters')

The first argument we passed to the re.match method is a regular expression.

The square brackets [] are used to indicate a set of characters.

The A-Z characters represent a ranges of uppercase ASCII letters.

The caret ^ matches the start of the string and the dollar sign $ matches the end of the string.

The plus + causes the regular expression to match 1 or more repetitions of the preceding character (the letter range).

In its entirety, the regular expression checks if the provided string only contains uppercase ASCII letters.

There is also a str.isascii() method but it doesn't satisfy our requirement for allowing only letters.

The str.isascii method returns True if the string is empty or all characters in the string are ASCII, otherwise False is returned.

main
print(''.isascii()) # ๐Ÿ‘‰๏ธ True print('BOBBY'.isascii()) # ๐Ÿ‘‰๏ธ True print('BOBBY HADZ'.isascii()) # ๐Ÿ‘‰๏ธ True print('BOBBY_HADZ!.?'.isascii()) # ๐Ÿ‘‰๏ธ True print('ะคะคะค'.isascii()) # ๐Ÿ‘‰๏ธ False

ASCII characters have code points in the range U+0000-U+007F.

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.

Copyright ยฉ 2022 Borislav Hadzhiev