w3resource

Python program to extract uppercase letters


2. Uppercase Extractor

Write a Python program that uses the filter function to extract all uppercase letters from a list of mixed-case strings.

Sample Solution:

Python Code:

# Define a list of mixed-case strings
mixed_case_strings = ["Hello", "w3resource", "Python", "Filter", "Learning"]

print("Original list of strings:\n",mixed_case_strings)
# Use the filter function to extract uppercase letters
uppercase_letters = list(filter(lambda char: char.isupper(), ''.join(mixed_case_strings)))

print("\nExtract all uppercase letters from the said list of mixed-case strings:")
# Print the extracted uppercase letters
print(uppercase_letters)

Explanation:

In the exercise above,

  • First, start with a list of mixed-case strings named mixed_case_strings.
  • Next, we use the filter function to filter characters from all strings and retain only uppercase letters. We do this by using a lambda function that checks if a character is uppercase using the isupper() method.
  • The filter function returns an iterable of uppercase letters, which we convert to a list.
  • Finally, we print the extracted uppercase letters.

Sample Output:

Original list of strings:
 ['Hello', 'w3resource', 'Python', 'Filter', 'Learning']

Extract all uppercase letters from the said list of mixed-case strings:
['H', 'P', 'F', 'L']

Flowchart:

Flowchart: Python program to extract uppercase letters.

For more Practice: Solve these Related Problems:

  • Write a Python function that filters out only the uppercase characters from each string in a list and returns a list of strings containing just the uppercase letters.
  • Write a Python program that applies the filter function on a single mixed-case string to extract all uppercase letters and then joins them into a new string.
  • Write a Python program to filter a list of strings, keeping only those strings that are entirely in uppercase using the filter function.
  • Write a Python function that uses filter to extract uppercase letters from each element of a list and then counts the total number of uppercase characters.

Python Code Editor:

Previous: Python function to filter even numbers.
Next:Python function to filter numbers by threshold.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.