Python String isupper() Method



The Python String isupper() method is used to determine whether all the case-based characters or the letters of the given string are in uppercase. The cased-characters are the ones having general category property being one of “Ll” (Letter, lowercase), “Lu” (Letter, uppercase), or “Lt” (Letter, title case).

Upper case is defined as the letters that are written or printed in their large forms or also known as capital letters.

For instance, if the upper case letters in the given word “ProGRaMmINg” are P, G, R M, I and N.

Note: Numbers, symbols and spaces in the given string are not checked, only the alphabet characters are considered.

Syntax

Following is the syntax of Python String isupper() method:

str.isupper()

Parameters

This method does not accept any parameter.

Return Value

This method returns true if all cased characters in the string are uppercase and there should be at least one cased character, otherwise it returns false.

Example

When we pass all the letters in the string in uppercase, it returns True.

In the following example a string ‘PYTHON’ is created with all the letters in upper case. The Python Sring isupper() method is then used to check whether the given string is in uppercase. The result is then retrieved.

# initializing the string
Given_string = "PYTHON"
# Checking whether the string is in uppercase
res = Given_string.isupper()
# Printing the result
print ("Is the given string in uppercase?: ", res)

Output of the above code is as follows:

Is the given string in uppercase?:  True

Example

If all the letters in the string is in lowercase, False is returned.

Following is an example where a string ‘this is string example....wow!!!’ is created. The result is then retrieved stating whether the given string is in uppercase using isupper() method.

# initialize the string
str = "this is string example....wow!!!";
print (str.isupper())

Following is the output of the above code:

False

Example

In the example given below the count of all the uppercase words in the given sentence is calculated using a for loop. Firstly, a string is created. Then the string is splitted and the result is retrieved by counting the uppercase words:

# initializing the string
Given_string = "python IS a PROGRAMMING language"
res = Given_string.split()
count = 0
# Counting the number of uppercase
for i in res:
   if (i.isupper()):
      count = count + 1
# Printing the result
print ("The number of uppercase words in the sentence is: ", str(count))

While executing the above code we get the following output:

The number of uppercase words in the sentence is:  2

Example

In the following example a string containing number, symbol and letters is created to check whether it is uppercase:

# initializing the string
Given_string = "Cod3iN#g"
# Checking whether the string is in uppercase
res = Given_string.isupper()
# Printing the result
print ("Is the given string in uppercase?: ", res)

When we run above program, it produces following result:

Is the given string in uppercase?:  False
python_strings.htm
Advertisements