The isupper()
method in Python is used to check whether all the alphabetic characters in a string are in uppercase. It returns True
if all the alphabetic characters in the string are in uppercase and there is at least one cased character (i.e., it does not return True
for an empty string). Otherwise, it returns False
.Here's the syntax of the isupper()
method:
string.isupper()
# Examples of isupper() method
string1 = "HELLO"
string2 = "Hello"
string3 = "12345"
string4 = ""
result1 = string1.isupper()
result2 = string2.isupper()
result3 = string3.isupper()
result4 = string4.isupper()
print("String 1:", string1)
print("String 2:", string2)
print("String 3:", string3)
print("String 4:", string4)
print("isupper() result for String 1:", result1)
print("isupper() result for String 2:", result2)
print("isupper() result for String 3:", result3)
print("isupper() result for String 4:", result4)
String 1: HELLO
String 2: Hello
String 3: 12345
String 4:
isupper() result for String 1: True
isupper() result for String 2: False
isupper() result for String 3: False
isupper() result for String 4: False
In this example, string1
contains only uppercase alphabetic characters ("HELLO"), so isupper()
returns True
. string2
contains both uppercase and lowercase characters, so it is not entirely in uppercase, and isupper()
returns False
. string3
contains only digits ("12345"), which are not alphabetic characters, so it returns False
as well. string4
is an empty string, and since it has no characters, isupper()
still returns False
.
The isupper()
method is useful when you want to check if a string is in uppercase or when you need to validate user input for uppercase strings. It can be helpful for text processing and input validation tasks where uppercase formatting is required.