Sometimes, while working with Python Strings, we can have a problem in which we need to remove all the words beginning with capital letters. Words that begin with capital letters are proper nouns and their occurrence mean different meaning to the sentence and can be sometimes undesired. Let’s discuss certain ways in which this task can be performed.
Input : test_str = ‘GeeksforGeeks is best for Geeks’
Output : ‘ is best for ‘
Input : test_str = ‘GeeksforGeeks Is Best For Geeks’
Output : ”
Method #1 : Using join() + split() + isupper()
The combination of the above functions can provide one of the ways in which this problem can be solved. In this, we perform the task of extracting individual strings with an upper case using isupper() and then perform join() to get the resultant result.
Python3
test_str = 'GeeksforGeeks is Best for Geeks'
print ( "The original string is : " + str (test_str))
temp = test_str.split()
res = " " .join([ele for ele in temp if not ele[ 0 ].isupper()])
print ( "The filtered string : " + str (res))
|
OutputThe original string is : GeeksforGeeks is Best for Geeks
The filtered string : is for
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using regex()
Using regex is one of the ways in which this problem can be solved. In this, we extract all the elements that are upper case using appropriate regex.
Python3
import re
test_str = 'GeeksforGeeks is Best for Geeks'
print ( "The original string is : " + str (test_str))
res = re.sub(r "\s*[A-Z]\w*\s*" , " " , test_str).strip()
print ( "The filtered string : " + str (res))
|
OutputThe original string is : GeeksforGeeks is Best for Geeks
The filtered string : is for
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Without using builtin methods
Python3
test_str = 'GeeksforGeeks is Best for Geeks'
print ( "The original string is : " + str (test_str))
temp = test_str.split()
capital = "ABCDEFGHIJKLMOPQRSTUVWXYZ"
res = " " .join([ele for ele in temp if not ele[ 0 ] in capital])
print ( "The filtered string : " + str (res))
|
OutputThe original string is : GeeksforGeeks is Best for Geeks
The filtered string : is for
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using join() + split() + isupper()+lambda functions
Python3
test_str = 'GeeksforGeeks is Best for Geeks'
print ( "The original string is : " + str (test_str))
temp = test_str.split()
res = " " .join( list ( filter ( lambda x: x[ 0 ].islower(), temp)))
print ( "The filtered string : " + str (res))
|
OutputThe original string is : GeeksforGeeks is Best for Geeks
The filtered string : is for
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #5: Using operator.countOf() method
Python3
import operator as op
test_str = 'GeeksforGeeks is Best for Geeks'
print ( "The original string is : " + str (test_str))
temp = test_str.split()
capital = "ABCDEFGHIJKLMOPQRSTUVWXYZ"
res = " " .join([ele for ele in temp if op.countOf(capital, ele[ 0 ]) = = 0 ])
print ( "The filtered string : " + str (res))
|
OutputThe original string is : GeeksforGeeks is Best for Geeks
The filtered string : is for
Time Complexity: O(N)
Auxiliary Space : O(N)
Method #6 : Using split(),ord(),join() methods
Python3
test_str = 'GeeksforGeeks is Best for Geeks'
print ( "The original string is : " + str (test_str))
temp = test_str.split()
res = []
for i in temp:
if not ord (i[ 0 ]) in range ( 65 , 90 ):
res.append(i)
res = " " .join(res)
print ( "The filtered string : " + str (res))
|
OutputThe original string is : GeeksforGeeks is Best for Geeks
The filtered string : is for
Time Complexity: O(N)
Auxiliary Space : O(N)
Method #7 : Using reduce():
Algorithm:
- Import reduce function from functools module
- Initialize the input string and capital letters string
- Use split() function to split the words in the string into a list
- Pass this list as a parameter to the reduce() function
- Initialize the accumulator with an empty string and the lambda function
- Check if the first character of the word is not in the capital letters string
- If yes, then add the word with a space to the accumulator
- If no, then add an empty string to the accumulator
- Return the final filtered string.
Python3
from functools import reduce
test_str = 'GeeksforGeeks is Best for Geeks'
print ( "The original string is : " + str (test_str))
capital = "ABCDEFGHIJKLMOPQRSTUVWXYZ"
res = reduce ( lambda acc, word: acc + ( " " +
word if not word[ 0 ] in capital else " "), test_str.split(), " ")
print ( "The filtered string : " + str (res))
|
OutputThe original string is : GeeksforGeeks is Best for Geeks
The filtered string : is for
Time Complexity: O(n), where n is the number of words in the input string. The split() function has a time complexity of O(n), and the reduce() function has a time complexity of O(n-1).
Auxiliary Space: O(n), where n is the number of words in the input string. The split() function creates a list of n words, and the reduce() function stores the filtered words in the accumulator.
Method #8 : Using itertools:
Algorithm :
- Initialize the test_str variable with a string.
- Split the string into a list of words using the split() method and store it in the temp variable.
- Create a set of capital letters and store it in the capital variable.
- Use a list comprehension to filter out words that start with a capital letter by iterating over each word in the temp list and checking if its first letter is in the capital set. Store the resulting list of filtered words in the res variable.
- Join the filtered words back into a string using the join() method and store it in the res variable.
- Print the original string and the filtered string.
Python3
import itertools
test_str = 'GeeksforGeeks is Best for Geeks'
print ( "The original string is : " + str (test_str))
temp = test_str.split()
capital = set ( 'ABCDEFGHIJKLMOPQRSTUVWXYZ' )
res = ' ' .join( filter ( lambda ele: ele[ 0 ] not in capital, temp))
print ( "The filtered string : " + str (res))
|
OutputThe original string is : GeeksforGeeks is Best for Geeks
The filtered string : is for
The time complexity : O(n), where n is the length of the input string. This is because the code iterates over each word in the string once, and the time it takes to check if a letter is in a set or to join a list of words into a string is constant time.
Auxiliary space: O(n), where n is the length of the input string. This is because the code creates a new list of words and a new string to store the filtered words, both of which have the same size as the original string. Additionally, the capital set has a constant size of 26, so it doesn’t contribute to the space complexity in this case