Skip to content
Related Articles

Related Articles

Python – Eliminate Capital Letter Starting words from String
  • Last Updated : 03 Jul, 2020

Sometimes, while working with Python Strings, we can have a problem in which we need to remove all the words with begin with capital letters. Words which begin with capital letters are proper nouns and their occurrence mean different meaning to 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 above functions can provide one of the way in which this problem can be solved. In this, we perform the task of extracting individual strings with upper case using isupper() and then perform join() to get the resultant result.




# Python3 code to demonstrate working of 
# Eliminate Capital Letter Starting words from String
# Using join() + split() + isupper()
      
# initializing string
test_str = 'GeeksforGeeks 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 : GeeksforGeeks is Best for Geeks
The filtered string : is for

 

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 code to demonstrate working of 
# Eliminate Capital Letter Starting words from String
# Using regex()
import re
      
# initializing string
test_str = 'GeeksforGeeks 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 : GeeksforGeeks is Best for Geeks
The filtered string : is for

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :