Sometimes, while working with strings, we are concerned about the case sensitivity of strings and might require getting just a specific case of character in a long string. Let’s discuss certain ways in which only uppercase letters can be extracted from a string.
Method #1: Using list comprehension + isupper()
List comprehension and isupper function can be used to perform this particular task. The list comprehension is primarily used to iterate over the list and isupper function checks for the uppercase characters.
Python3
# Python3 code to demonstrate working of # Extract Upper Case Characters # Using list comprehension + isupper() # initializing string test_str = " GeeksForGeeKs & quot # printing original string print (& quot The original string is : & quot + str (test_str)) # Extract Upper Case Characters # Using list comprehension + isupper() res = [char for char in test_str if char.isupper()] # printing result print (& quot The uppercase characters in string are : & quot + str (res)) |
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']
Time Complexity: O(n)
Space Complexity: O(n)
Method #2: Using filter() + lambda
Filter function along with lambda functionality can be used to perform this particular task. The filter function performs the specific selection of case characters and lambda function is used for string traversal.
Python3
# Python3 code to demonstrate working of # Extract Upper Case Characters # Using filter() + lambda # initializing string test_str = " GeeksForGeeKs & quot # printing original string print (& quot The original string is : & quot + str (test_str)) # Extract Upper Case Characters # Using filter() + lambda res = list ( filter ( lambda c: c.isupper(), test_str)) # printing result print (& quot The uppercase characters in string are : & quot + str (res)) |
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']
Time Complexity: O(n)
Space Complexity: O(n)
Method #3: Without using builtin methods
Python3
# Python3 code to demonstrate working of # Extract Upper Case Characters # initializing string test_str = "GeeksForGeeKs" # printing original string print ( "The original string is : " + str (test_str)) # Extract Upper Case Characters upperalpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" res = [] for i in test_str: if i in upperalpha: res.append(i) # printing result print ( "The uppercase characters in string are : " + str (res)) |
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']
Time Complexity: O(n)
Space Complexity: O(n)
Method #4: Using ord() method.The ASCII value of the uppercase alphabet is from 65 to 90.
Python3
# Python3 code to demonstrate working of # Extract Upper Case Characters # initializing string test_str = "GeeksForGeeKs" # printing original string print ( "The original string is : " + str (test_str)) # Extract Upper Case Characters res = [] for i in test_str: if ord (i) in range ( 65 , 91 ): res.append(i) # printing result print ( "The uppercase characters in string are : " + str (res)) |
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']
Time Complexity: O(n)
Space Complexity: O(n)
Method #5 : Using replace() and list() methods
Python3
# Python3 code to demonstrate working of # Extract Upper Case Characters # initializing string test_str = "GeeksForGeeKs" # printing original string print ( "The original string is : " + str (test_str)) # Extract Upper Case Characters loweralpha = "abcdefghijklmnopqrstuvwxyz" for i in test_str: if i in loweralpha: test_str = test_str.replace(i, "") res = list (test_str) # printing result print ( "The uppercase characters in string are : " + str (res)) |
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']
Time complexity: O(n)
Auxiliary space: O(n)
Method #6 : Using operator.countOf() method
Python3
# Python3 code to demonstrate working of # Extract Upper Case Characters import operator as op # initializing string test_str = "GeeksForGeeKs" # printing original string print ( "The original string is : " + str (test_str)) # Extract Upper Case Characters upperalpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" res = [] for i in test_str: if op.countOf(upperalpha, i) > 0 : res.append(i) # printing result print ( "The uppercase characters in string are : " + str (res)) |
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']
Time Complexity: O(n)
Space Complexity: O(n)
Method #7: Using regular expressions
Python3
import re # initializing string test_str = "GeeksForGeeKs" # printing original string print ( "The original string is : " + str (test_str)) # Extract Upper Case Characters using regular expressions res = re.findall(r '[A-Z]' , test_str) # printing result print ( "The uppercase characters in string are : " + str (res)) #This code is contributed by Vinay Pinjala. |
The original string is : GeeksForGeeKs The uppercase characters in string are : ['G', 'F', 'G', 'K']
Time Complexity: O(N)
Auxiliary Space : O(N)
Method #8: Using reduce() method:
This code demonstrates how to extract all uppercase characters from a given string using the reduce function and a lambda function in Python.
The code starts by initializing a string called test_str with some random characters, and then defines a function to extract all uppercase characters from the string.
The reduce function is then used to apply the lambda function to each character in the string. The lambda function takes two arguments, x and y, which represent the accumulated result and the current character, respectively. If the current character y is uppercase, the lambda function adds it to the result x (which is initially an empty list) and returns the updated result. If the current character y is not uppercase, the lambda function simply returns the accumulated result x without any modifications.
Finally, the result is printed as a list of uppercase characters that were extracted from the input string.
Overall, the code provides a simple and efficient way to extract all uppercase characters from a given string in Python.
Python3
from functools import reduce # initializing string test_str = "GeeksForGeeKs" # printing original string print ( "The original string is : " + str (test_str)) # using reduce function and lambda function to extract uppercase characters result = list ( reduce ( lambda x, y: x + [y] if y.isupper() else x, test_str, [])) # printing the result print ( "The uppercase characters in string are:" , result) #This code is contributed by Jyothi pinjala. |
The original string is : GeeksForGeeKs The uppercase characters in string are: ['G', 'F', 'G', 'K']
The time complexity of the code is O(n), where n is the length of the input string test_str. This is because the reduce function applies the lambda function to each character in the string exactly once, so the time required to process the entire string is proportional to its length.
The space complexity of the code is also O(n), where n is the length of the input string test_str. This is because the reduce function builds a list of uppercase characters, which may be as long as the entire input string in the worst case. The space required to store this list is proportional to the length of the input string.