Given list of Strings, task is to add a space before sequence which begin with capital letters.
Input : test_list = [“gfgBest”, “forGeeks”, “andComputerScienceStudents”]
Output : [‘gfg Best’, ‘for Geeks’, ‘and Computer Science Students’]
Explanation : Words segregated by Capitals.Input : test_list = [“ComputerScienceStudentsLoveGfg”]
Output : [‘Computer Science Students Love Gfg’]
Explanation : Words segregated by Capitals.
Method #1 : Using loop + join()
This is one of the ways in which this task can be perfomed. In this, we perform the task of iterating all the stings and then all the characters before adding space using loop in brute force manner. The isupper() is used to check for capital character.
Python3
# Python3 code to demonstrate working of # Add Space between Potential Words # Using loop + join() # initializing list test_list = ["gfgBest", "forGeeks", "andComputerScience"] # printing original list print("The original list : " + str(test_list)) res = [] # loop to iterate all strings for ele in test_list: temp = [[]] for char in ele: # checking for upper case character if char.isupper(): temp.append([]) # appending character at latest list temp[-1].append(char) # joining lists after adding space res.append(' '.join(''.join(ele) for ele in temp)) # printing result print("The space added list of strings : " + str(res)) |
The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
Method #2 : Using regex() + list comprehension
The combination of above functions can also be used to solve this problem. In this we employ regex code to check for upper case letter and perform space addition and joining using list comprehension.
Python3
# Python3 code to demonstrate working of # Add Space between Potential Words # Using regex() + list comprehension import re # initializing list test_list = ["gfgBest", "forGeeks", "andComputerScience"] # printing original list print("The original list : " + str(test_list)) # using regex() to perform task res = [re.sub(r"(\w)([A-Z])", r"\1 \2", ele) for ele in test_list] # printing result print("The space added list of strings : " + str(res)) |
The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
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.
Recommended Posts:
- Python - Add space between Numbers and Alphabets in String
- Regex in Python to put spaces between words starting with capital letters
- Python - Remove space between tuple elements
- Minimum Distance Between Words of a String
- How to insert a space between characters of all the elements of a given NumPy array?
- Python | Add comma between numbers
- Python - Add K between case shifts
- Scraping And Finding Ordered Words In A Dictionary using Python
- Possible Words using given characters in Python
- Find the k most frequent words from data set in Python
- Sort the words in lexicographical order in Python
- Python Counter to find the size of largest subset of anagram words
- Python | Remove all duplicates words from a given sentence
- Python | Sort words of sentence in ascending order
- Part of Speech Tagging with Stop words using NLTK in python
- Get similar words suggestion using Enchant in Python
- Python program to print even length words in a string
- Python | Stemming words with NLTK
- Python program to find uncommon words from two Strings
- Python program to count words in a sentence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

