Python isupper() Method [with Examples]

Case sensitive refers to the fact that "A" differs from "a". A Python program differentiates between "a" and "A." Python requires close attention to the string's case while developing code. Numerous helpful built-in functions in the Python programming language check the case of the supplied strings. The Python functions lower(), islower(), upper(), and isupper() are a few that can be used with strings. In this short article, we will learn how we can use the Python isupper method to check if the string is in upper case or not by solving various examples.

 

Introduction to Python isupper() method

The Python standard library has a built-in function called Python isupper() method. It supports the use of strings and other types of data. It shows whether a string of characters contains only capital letters or not. If at least one character is lowercase, it returns FALSE. Otherwise, if every letter in the string is capitalized, it returns TRUE.

Advertisement

The following is the syntax of the Python isupper method:

String.isupper()

The string is the name of the variable where you have stored the string. The isupper method checks whether the given string is in upper case letters or not.

 

Example 1: Using python isupper()

Now we will take three different strings and apply the Python isupper method to check whether the given strings are in upper case or not. See the example below;

# creating different strings
str1 = "I AM UPPERCASE"
str2 = "i am lowercase"
str3 = "I am MixedCase"

# checking the string using python isupper
print ("Str1 is upper case: ", str1.isupper())
print ("Str2 is upper case: ", str2.isupper())      
print ("Str3 is upper case: ", str3.isupper())       

Output:

Str1 is upper case:  True
Str2 is upper case:  False
Str3 is upper case:  False

As you can see, only the first string is True because all the alphabets are in upper case while we get False for string two and string three as they have some lower case alphabets.

 

Example 2: Check the string case from the user input

Now we will take a string input from the user and will check if the input from the user is in upper case or not. We will use the if-else statement to check the string.

Advertisement
# taking input from user
str4 = input("Enter your String : ")

# printing
print('Is the input upper case? ')

# checking the input using python isupper
if str4.isupper() == True:
    print("YES")
else:
    print("NO")

Output:

Python-isupper

As you can see, I entered all the letters in the upper case which is why we get Yes. If you will enter something in lowercase, the program will print No.

 

Example3: Count total upper case words

So far, we demonstrated only simple examples to understand the function of the isupper() method of the standard Python library. However, this function can be used with any complex and difficult problems. It has numerous practical uses in multiple areas.

Now, we will use the split function along with the isupper() method to count the total number of words which are in upper case in the given sentence. First, we will define a string and then will use the split function to split the sentence into words. Finally, we will use the for loop to iterate through the words and

# creating a string
test = "THIS is BASHIR alam, MAjoring IN computer SECINCE"

# using split function to split the sentence
liststr = test.split()

# total count
count = 0

# for loop to iterate through the words
for i in liststr:
    if (i.isupper()):
        count = count + 1
print ("Total upper case words in the string are: " + str(count))

count all those which are in upper case.

Output:

Advertisement
Total upper case words in the string are: 4
As shown, we get 4 because there are four words that are in upper case.

Example 4: Convert words to uppercase

Extending the previous example, we iterate through each item in the string. If a specified item is already in upper case, simply print it. If it is not, convert it into the upper case and return the string in the upper case.

# creating a string
test = "THIS is BASHIR alam, MAjoring IN computer SECINCE"

# using split function to split the sentence
liststr = test.split()

# total count
count = 0
lower = 0
# for loop to iterate through the words
for i in liststr:
    if (i.isupper()):
        count = count + 1
        print("The item is upper case : ",i)
    else:
        lower = lower+1
        print("The item is converted to upper case : ",i.upper())
       
print('\n--------------------summary-----------------------------------')
print ("Total upper case words in the string are: " + str(count))
print ("Total upper case words in the string are: " + str(lower))
print("\nThe string after conversion: ",test.upper())

Output:

The item is upper case :  THIS
The item is converted to upper case :  IS
The item is upper case :  BASHIR
The item is converted to upper case :  ALAM,
The item is converted to upper case :  MAJORING
The item is upper case :  IN
The item is converted to upper case :  COMPUTER
The item is upper case :  SECINCE

--------------------summary-----------------------------------
Total upper case words in the string are: 4
Total upper case words in the string are: 4

The string after conversion:  THIS IS BASHIR ALAM, MAJORING IN COMPUTER SECINCE

As you can see, we get full details about the lower case and upper case words and the above program has converted the lower case words to upper case.

 

Summary

The Python isupper() method checks whether all the characters of a given string are uppercase or not. It returns True if all characters are uppercase and False even if one character is not in uppercase. In this short article, we learned how we can use the python isupper method to check if the letter or word is in upper case. We solved various examples using Python.

 

Further reading

Python isupper
Python string

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X