How to Check If a String Is Uppercase in Python

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a string is entirely in uppercase using Python. The lab begins by exploring uppercase strings and creating a Python file to experiment with different string examples. You will then use the isupper() method, a built-in string method, to determine if all characters in a string are uppercase.

Finally, the lab will guide you on how to account for non-letter characters when checking for uppercase strings, ensuring accurate results even when strings contain numbers or spaces. This involves modifying the code to iterate through the string and check each character individually.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python/BasicConceptsGroup -.-> python/strings("Strings") python/FunctionsGroup -.-> python/function_definition("Function Definition") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") subgraph Lab Skills python/strings -.-> lab-559581{{"How to Check If a String Is Uppercase in Python"}} python/function_definition -.-> lab-559581{{"How to Check If a String Is Uppercase in Python"}} python/build_in_functions -.-> lab-559581{{"How to Check If a String Is Uppercase in Python"}} end

Explore Uppercase Strings

In this step, you will learn about uppercase strings in Python and how to identify them. Understanding how to work with uppercase strings is fundamental for tasks like data validation, text normalization, and command processing.

Let's start by creating a Python file to experiment with strings.

  1. Open the VS Code editor in the LabEx environment.

  2. Create a new file named uppercase_strings.py in the ~/project directory.

    You can create a new file by clicking on "File" -> "New File" in the VS Code menu, and then save it as uppercase_strings.py in the ~/project directory.

  3. Add the following Python code to the uppercase_strings.py file:

## Example strings
string1 = "HELLO"
string2 = "Hello"
string3 = "123HELLO"
string4 = "HELLO WORLD"

## Print the strings
print(f"String 1: {string1}")
print(f"String 2: {string2}")
print(f"String 3: {string3}")
print(f"String 4: {string4}")

This code defines four strings: string1, string2, string3, and string4. The print() function is used to display these strings.

Now, let's run the script to see the output.

  1. Open a terminal in the VS Code environment.

    You can open a terminal by clicking on "View" -> "Terminal" in the VS Code menu.

  2. Execute the Python script using the following command:

python uppercase_strings.py

You should see the following output:

String 1: HELLO
String 2: Hello
String 3: 123HELLO
String 4: HELLO WORLD

In the next step, you will learn how to use the isupper() method to check if a string is entirely in uppercase.

Use isupper() Method

In this step, you will learn how to use the isupper() method in Python to check if a string is entirely in uppercase. The isupper() method is a built-in string method that returns True if all characters in the string are uppercase, and False otherwise.

Let's modify the uppercase_strings.py file you created in the previous step to use the isupper() method.

  1. Open the uppercase_strings.py file in the VS Code editor.
  2. Modify the code to include the isupper() method as follows:
## Example strings
string1 = "HELLO"
string2 = "Hello"
string3 = "123HELLO"
string4 = "HELLO WORLD"

## Check if the strings are uppercase using isupper()
result1 = string1.isupper()
result2 = string2.isupper()
result3 = string3.isupper()
result4 = string4.isupper()

## Print the results
print(f"String 1: {string1}, is uppercase: {result1}")
print(f"String 2: {string2}, is uppercase: {result2}")
print(f"String 3: {string3}, is uppercase: {result3}")
print(f"String 4: {string4}, is uppercase: {result4}")

In this code, we are calling the isupper() method on each of the example strings and storing the results in variables result1, result2, result3, and result4. Then, we print the original strings along with their corresponding isupper() results.

Now, let's run the script to see the output.

  1. Open a terminal in the VS Code environment.
  2. Execute the Python script using the following command:
python uppercase_strings.py

You should see the following output:

String 1: HELLO, is uppercase: True
String 2: Hello, is uppercase: False
String 3: 123HELLO, is uppercase: False
String 4: HELLO WORLD, is uppercase: False

As you can see, string1 (HELLO) is the only string that returns True because all its characters are uppercase. The other strings return False because they contain lowercase characters, numbers, or spaces.

In the next step, you will learn how to account for non-letter characters when checking for uppercase strings.

Account for Non-Letters

In this step, you will learn how to account for non-letter characters when checking if a string is considered uppercase. The isupper() method only returns True if all characters are uppercase letters. If a string contains numbers, spaces, or other non-letter characters, isupper() will return False. To handle such cases, you can filter out the non-letter characters before applying the isupper() method.

Let's modify the uppercase_strings.py file to account for non-letter characters.

  1. Open the uppercase_strings.py file in the VS Code editor.
  2. Modify the code to include a filtering mechanism for non-letter characters as follows:
## Example strings
string1 = "HELLO"
string2 = "Hello"
string3 = "123HELLO"
string4 = "HELLO WORLD"
string5 = "HELLO123WORLD"

## Function to check if a string is uppercase, ignoring non-letters
def is_uppercase_ignore_non_letters(s):
    letters = ''.join(filter(str.isalpha, s))
    return letters.isupper()

## Check if the strings are uppercase using the custom function
result1 = is_uppercase_ignore_non_letters(string1)
result2 = is_uppercase_ignore_non_letters(string2)
result3 = is_uppercase_ignore_non_letters(string3)
result4 = is_uppercase_ignore_non_letters(string4)
result5 = is_uppercase_ignore_non_letters(string5)

## Print the results
print(f"String 1: {string1}, is uppercase (ignoring non-letters): {result1}")
print(f"String 2: {string2}, is uppercase (ignoring non-letters): {result2}")
print(f"String 3: {string3}, is uppercase (ignoring non-letters): {result3}")
print(f"String 4: {string4}, is uppercase (ignoring non-letters): {result4}")
print(f"String 5: {string5}, is uppercase (ignoring non-letters): {result5}")

In this code:

  • We define a function is_uppercase_ignore_non_letters(s) that filters out non-letter characters from the input string s using filter(str.isalpha, s). The str.isalpha() method returns True if a character is a letter (A-Z, a-z), and False otherwise.
  • The ''.join() method joins the filtered letters back into a string.
  • We then apply the isupper() method to the filtered string.
  • Finally, we print the original strings along with their corresponding results from the custom function.

Now, let's run the script to see the output.

  1. Open a terminal in the VS Code environment.
  2. Execute the Python script using the following command:
python uppercase_strings.py

You should see the following output:

String 1: HELLO, is uppercase (ignoring non-letters): True
String 2: Hello, is uppercase (ignoring non-letters): False
String 3: 123HELLO, is uppercase (ignoring non-letters): True
String 4: HELLO WORLD, is uppercase (ignoring non-letters): False
String 5: HELLO123WORLD, is uppercase (ignoring non-letters): True

As you can see, string3 (123HELLO) and string5 (HELLO123WORLD) now return True because the non-letter characters are ignored, and the remaining letters are all uppercase.

This approach allows you to accurately determine if a string is uppercase, even when it contains non-letter characters.

Summary

In this lab, you began by exploring uppercase strings in Python and creating a uppercase_strings.py file to experiment with them. You defined four strings with varying capitalization and content, then printed them to the console using the print() function. This allowed you to visually inspect different string formats and prepare for using the isupper() method in the next step.

The next step will focus on utilizing the isupper() method to programmatically determine if a string consists entirely of uppercase characters.