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 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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.