Python String isupper() Method – Tutorial with Examples

Python String isupper() Method

Python string is a sequence of characters. The isupper() method is a built-in method in Python that can be used to check whether all the characters in a given string are uppercase or not. It returns True if all characters are in uppercase, else returns False.

Syntax

The syntax of the isupper() method is:

string.isupper()

Here, string is the string which we want to check for uppercase characters.

Return Value

The isupper() method returns a boolean value. It returns True if all the characters in the given string are uppercase, else returns False.

Examples

Here are three different examples of how to use the isupper() method in Python:

Example 1: Check if the given string contains only uppercase letters

string = "HELLO, WORLD!"
result = string.isupper()
print(result)

Output:

True

In this example, we check if the given string “HELLO, WORLD!” contains only uppercase letters using the isupper() method. Since all the letters in the given string are in uppercase, it returns True.

Example 2: Check if the given string contains a mixture of uppercase and lowercase letters

string = "Hello, World!"
result = string.isupper()
print(result)

Output:

False

In this example, we check if the given string “Hello, World!” contains only uppercase letters using the isupper() method. Since the given string contains both uppercase and lowercase letters, it returns False.

Example 3: Check if the given string contains numbers and special characters

string = "PYTHON3 IS #1"
result = string.isupper()
print(result)

Output:

True

In this example, we check if the given string “PYTHON3 IS #1” contains only uppercase letters using the isupper() method. Although the string contains numbers and special characters, since all the letters in the given string are in uppercase, it returns True.

Use Cases

The isupper() method can be useful in a variety of scenarios, such as:

  • Validating user input to ensure that it only contains uppercase characters
  • Checking if a given string contains only uppercase characters
  • Converting a mixed-case string to uppercase using the upper() method and checking if the string was successfully converted

Leave a Reply

Your email address will not be published. Required fields are marked *