The isupper()
method is a built-in method in Python that is used to determine whether all the characters in a string are uppercase letters. It returns True if all the characters in the string are uppercase, and False otherwise.
Syntax:
string.isupper()
How to use the isupper()
method:
string1 = "HELLO"
string2 = "Hello"
string3 = "123"
string4 = "WORLD123"
print(string1.isupper()) # Output: True
print(string2.isupper()) # Output: False
print(string3.isupper()) # Output: False
print(string4.isupper()) # Output: True
Example 1
Checking Acronyms.
def is_acronym(word):
if word.isupper():
print(f"{word} is an acronym.")
else:
print(f"{word} is not an acronym.")
is_acronym("NASA") # Output: NASA is an acronym.
is_acronym("Python") # Output: Python is not an acronym.
Example 2
Checking all characters are uppercase.
string = "HELLO"
if string.isupper():
print("All characters are uppercase.")
else:
print("Not all characters are uppercase.")
Example 3
List comprehension to filter out the strings that are entirely uppercase by applying the isupper()
method on each element.
strings = ["HELLO", "WORLD", "Hello", "world"]
uppercase_strings = [s for s in strings if s.isupper()]
print(uppercase_strings)