Python – Add Space between Potential Words
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 performed. 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 listtest_list = ["gfgBest", "forGeeks", "andComputerScience"]# printing original listprint("The original list : " + str(test_list))res = []# loop to iterate all stringsfor 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 resultprint("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 letters and perform space addition and joining using list comprehension.
Python3
# Python3 code to demonstrate working of# Add Space between Potential Words# Using regex() + list comprehensionimport re# initializing listtest_list = ["gfgBest", "forGeeks", "andComputerScience"]# printing original listprint("The original list : " + str(test_list))# using regex() to perform taskres = [re.sub(r"(\w)([A-Z])", r"\1 \2", ele) for ele in test_list]# printing resultprint("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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


