W3Basic Logo

Python String isupper() Method

The str.isupper() method returns True if all the characters in a string are in uppercase. If it finds at least one of the characters in lowercase, it returns False.

Example:

text ="PYTHON IS AWESOME"
print(text.isupper())

Output

True

Syntax

The Syntax of the str.isupper() function is:

str.isupper()

Here, str is a string.

Parameters

The str.isupper() function does not take any parameters.

Return Value

The str.isupper() function returns:

  • True if all the characters in the string are in uppercase
  • False if at least one of the characters in the string is in lowercase

Example 1: Python Program to check if the string is in uppercase

The isupper() method returns:

  • True: If all the characters in the string is uppercase
  • True: If the string has a combination of numbers and uppercase characters
  • True: If the string has a combination of numbers, special case, and uppercase characters
  • False: If the string has one or more lowercase character
  • False: If the string has only whitespace or numbers
# Uppercase string
text ="PYTHON"
print(text.isupper())

# string with numbers
text ="PYTHON3 IS RELEASED IN THE YEAR 2008"
print(text.isupper())

# string with special chars
text ="PYTHON ROCKS !!!"
print(text.isupper())

# titlecase string
text ="Python"
print(text.isupper())

# lowercase string
text ="python"
print(text.isupper())

# Mixed Case string
text ="Welcome To Python Tutorial"
print(text.isupper())

# String with only numbers
text ="2022"
print(text.isupper())

Output

True
True
True
False
False
False
False

Example 2: How to use isupper() function in a Program?

In the real-world use case, we can use isupper() method to check if the given string consists of only uppercase characters. Here are a few example where uppercase strings are preferred:

  • Check First name and Last name (Block letters)
  • Days and Month names
  • Abbreviations, Certain Brand names, etc
# Method to verify if the text is uppercase
def check_upper(text):
    if text.isupper() == True:
        return "All the characters in the sentence are in uppercase"
    else:
        return "one or more characters in the sentence are not in uppercase"

# All characters are in uppercase
print(check_upper("THERE ARE SEVEN CONTINENTS IN THE WORLD"))

# some characters are in lowercase
print(check_upper("There are Seven Continents in the World."))

Output

All the characters in the sentence are in uppercase
one or more characters in the sentence are not in uppercase

Reference: Python Official String Methods

© 2023 W3Basic. All rights reserved.

Follow Us: