Skip to content
Related Articles

Related Articles

Python program to split a string by the given list of strings

View Discussion
Improve Article
Save Article
  • Last Updated : 16 Aug, 2022

Given a list of strings. The task is to split the string by the given list of strings. 

Input : test_str = ‘geekforgeeksbestforgeeks’, sub_list = [“best”] 
Output : [‘geekforgeeks’, ‘best’, ‘forgeeks’] 
Explanation : “best” is extracted as different list element.

Input : test_str = ‘geekforgeeksbestforgeeksCS’, sub_list = [“best”, “CS”] 
Output : [‘geekforgeeks’, ‘best’, ‘forgeeks’, “CS”] 
Explanation : “best” and “CS” are extracted as different list element. 

Method : Using re.split() + | operator

In this, we perform the task of split using regex split() with | operator to check for all the words that need to be put separately.


# Python3 code to demonstrate working of
# Separate specific Strings
# Using re.split() + | operator
import re
# initializing string
test_str = 'geekforgeeksisbestforgeeks'
# printing original String
print("The original string is : " + str(test_str))
# initializing list words
sub_list = ["best"]
# regex to for splits()
# | operator to include all strings
temp = re.split(rf"({'|'.join(sub_list)})", test_str)
res = [ele for ele in temp if ele]
# printing result
print("The segmented String : " + str(res))


The original string is : geekforgeeksisbestforgeeks
The segmented String : ['geekforgeeksis', 'best', 'forgeeks']

Time Complexity: O(n)

Auxiliary Space: O(n)

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!