Python – All possible space joins in String

22 Apr, 2020
Sometimes, while working with Python Strings, we can have a problem in which we need to construct strings with single space at every possible word ending. This kind of application can occur in domains in which we need to perform testing. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop + join()This is brute force way in which this task can be performed. In this, we perform the task of forming all possible joins using join() and task of iterating through all strings using loop.
# Python3 code to demonstrate working of # All possible space joins in String# Using loop + join()  # initializing stringtest_str = 'Geeksforgeeks is best for geeks'  # printing original stringprint("The original string is : " + str(test_str))  # All possible space joins in String# Using loop + join()res = []temp = test_str.split(' ')             strt_idx = 0lst_idx = len(temp)for idx in range(len(temp)-1):           frst_wrd = "".join(temp[strt_idx : idx + 1])       scnd_wrd = "".join(temp[idx + 1 : lst_idx])    res.append(frst_wrd + " " + scnd_wrd)      # printing result print("All possible spaces List : " + str(res)) 
The original string is : Geeksforgeeks is best for geeksAll possible spaces List : [‘Geeksforgeeks isbestforgeeks’, ‘Geeksforgeeksis bestforgeeks’, ‘Geeksforgeeksisbest forgeeks’, ‘Geeksforgeeksisbestfor geeks’]
 
Method #2 : Using enumerate() + join() + combinations()The combination of above methods can be used to perform this task. In this, we perform the task of extracting combinations using combinations(). This prints all combinations of all occurrences of spaces rather than just one as in above method.
# Python3 code to demonstrate working of # All possible space joins in String# Using enumerate() + join() + combinations()import itertools  # initializing stringtest_str = 'Geeksforgeeks is best for geeks'  # printing original stringprint("The original string is : " + str(test_str))  # All possible space joins in String# Using enumerate() + join() + combinations()res = []temp = test_str.split(' ')N = range(len(temp) - 1)for idx in N:    for sub in itertools.combinations(N, idx + 1):        temp1 = [val + " " if i in sub else val for i, val in enumerate(temp)]        temp2 = "".join(temp1)        res.append(temp2)      # printing result print("All possible spaces List : " + str(res)) 
The original string is : Geeksforgeeks is best for geeksAll possible spaces List : [‘Geeksforgeeks isbestforgeeks’, ‘Geeksforgeeksis bestforgeeks’, ‘Geeksforgeeksisbest forgeeks’, ‘Geeksforgeeksisbestfor geeks’, ‘Geeksforgeeks is bestforgeeks’, ‘Geeksforgeeks isbest forgeeks’, ‘Geeksforgeeks isbestfor geeks’, ‘Geeksforgeeksis best forgeeks’, ‘Geeksforgeeksis bestfor geeks’, ‘Geeksforgeeksisbest for geeks’, ‘Geeksforgeeks is best forgeeks’, ‘Geeksforgeeks is bestfor geeks’, ‘Geeksforgeeks isbest for geeks’, ‘Geeksforgeeksis best for geeks’, ‘Geeksforgeeks is best for geeks’]
Python string-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python | os.path.join() method
Introduction To PYTHON
Python OOPs Concepts
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary
Python Program for Fibonacci numbers