Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Eliminate Capital Letter Starting words from String

Python – Eliminate Capital Letter Starting words from String

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 = ‘Lazyroar is best for Geeks’ 
Output : ‘ is best for ‘
Input : test_str = ‘Lazyroar 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




# Python3 code to demonstrate working of
# Eliminate Capital Letter Starting words from String
# Using join() + split() + isupper()
 
# initializing string
test_str = 'Lazyroar is Best for Geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# Eliminate Capital Letter Starting words from String
# Using join() + split() + isupper()
temp = test_str.split()
res = " ".join([ele for ele in temp if not ele[0].isupper()])
 
# printing result
print("The filtered string : " + str(res))


Output

The original string is : Lazyroar 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




# Python3 code to demonstrate working of
# Eliminate Capital Letter Starting words from String
# Using regex()
import re
     
# initializing string
test_str = 'Lazyroar is Best for Geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# Eliminate Capital Letter Starting words from String
# Using regex()
res = re.sub(r"\s*[A-Z]\w*\s*", " ", test_str).strip()
 
# printing result
print("The filtered string : " + str(res))


Output

The original string is : Lazyroar is Best for Geeks
The filtered string : is for

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3: Without using builtin methods

Python3




# Python3 code to demonstrate working of
# Eliminate Capital Letter Starting words from String
 
# initializing string
test_str = 'Lazyroar is Best for Geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# Eliminate Capital Letter Starting words from String
temp = test_str.split()
capital="ABCDEFGHIJKLMOPQRSTUVWXYZ"
res = " ".join([ele for ele in temp if not ele[0] in capital])
 
# printing result
print("The filtered string : " + str(res))


Output

The original string is : Lazyroar 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




# Python3 code to demonstrate working of
# Eliminate Capital Letter Starting words from String
# Using join() + split() + isupper()
 
# initializing string
test_str = 'Lazyroar is Best for Geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# Eliminate Capital Letter Starting words from String
# Using join() + split() + isupper()+lambda functions
temp = test_str.split()
res = " ".join(list(filter(lambda x: x[0].islower(), temp)))
 
# printing result
print("The filtered string : " + str(res))


Output

The original string is : Lazyroar is Best for Geeks
The filtered string : is for

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #5: Using operator.countOf() method

Python3




# Python3 code to demonstrate working of
# Eliminate Capital Letter Starting words from String
import operator as op
# initializing string
test_str = 'Lazyroar is Best for Geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# Eliminate Capital Letter Starting words from String
temp = test_str.split()
capital = "ABCDEFGHIJKLMOPQRSTUVWXYZ"
res = " ".join([ele for ele in temp if op.countOf(capital, ele[0]) == 0])
 
# printing result
print("The filtered string : " + str(res))


Output

The original string is : Lazyroar 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




# Python3 code to demonstrate working of
# Eliminate Capital Letter Starting words from String
 
# initializing string
test_str = 'Lazyroar is Best for Geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# Eliminate Capital Letter Starting words from String
temp = test_str.split()
res = []
for i in temp:
    if not ord(i[0]) in range(65, 90):
        res.append(i)
res = " ".join(res)
# printing result
print("The filtered string : " + str(res))


Output

The original string is : Lazyroar is Best for Geeks
The filtered string : is for

Time Complexity: O(N)
Auxiliary Space : O(N)

Method #7 : Using reduce():

Algorithm:

  1. Import reduce function from functools module
  2. Initialize the input string and capital letters string
  3. Use split() function to split the words in the string into a list
  4. Pass this list as a parameter to the reduce() function
  5. Initialize the accumulator with an empty string and the lambda function
  6. Check if the first character of the word is not in the capital letters string
  7. If yes, then add the word with a space to the accumulator
  8. If no, then add an empty string to the accumulator
  9. Return the final filtered string.

Python3




from functools import reduce
 
test_str = 'Lazyroar is Best for Geeks'
# printing original string
print("The original string is : " + str(test_str))
 
 
# Eliminate Capital Letter Starting words from String
capital = "ABCDEFGHIJKLMOPQRSTUVWXYZ"
res = reduce(lambda acc, word: acc + (" " +
                                      word if not word[0] in capital else ""), test_str.split(), "")
 
# printing result
print("The filtered string : " + str(res))
# This code is contributed by Rayudu.


Output

The original string is : Lazyroar 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 :

  1. Initialize the test_str variable with a string.
  2. Split the string into a list of words using the split() method and store it in the temp variable.
  3. Create a set of capital letters and store it in the capital variable.
  4. 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.
  5. Join the filtered words back into a string using the join() method and store it in the res variable.
  6. Print the original string and the filtered string.

Python3




import itertools
 
# initializing string
test_str = 'Lazyroar is Best for Geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# Eliminate Capital Letter Starting words from String
temp = test_str.split()
capital = set('ABCDEFGHIJKLMOPQRSTUVWXYZ')
res = ' '.join(filter(lambda ele: ele[0] not in capital, temp))
 
# printing result
print("The filtered string : " + str(res))
 
# This code is contributed by Jyothi pinjala.


Output

The original string is : Lazyroar 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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments