Python program to split a string by the given list of strings
Last Updated :
08 Apr, 2023
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
# 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))
OutputThe original string is : geekforgeeksisbestforgeeks
The segmented String : ['geekforgeeksis', 'best', 'forgeeks']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method : Using replace(),split() methods
Approach
- Initiate a for loop to traverse list of strings
- Replace the string in original string by appending "*" front and back
- Finally split the string with * as parameter
- This will return a list, display the list
Python3
# Python3 code to demonstrate working of
# Separate specific Strings
# initializing string
test_str = 'geekforgeeksisbestforgeeks'
# printing original String
print("The original string is : " + str(test_str))
# initializing list words
sub_list = ["best"]
for i in sub_list:
test_str=test_str.replace(i,"*"+i+"*")
res=test_str.split("*")
# printing result
print("The segmented String : " + str(res))
OutputThe original string is : geekforgeeksisbestforgeeks
The segmented String : ['geekforgeeksis', 'best', 'forgeeks']
Time Complexity : O(N)
Auxiliary Space : O(N)
Method 3 : Use the re module
step by step approach :
- The program starts by importing the "re" module. This module provides regular expression operations in Python.
- The program then initializes a string variable named "test_str" with the value "geekforgeeksisbestforgeeks".
- The program then prints the original string using the "print" statement and concatenating the original string with a string literal.
- The program initializes a list variable named "sub_list" with the value ["best"].
- The program creates a regular expression pattern by joining the elements of the "sub_list" with a pipe symbol "|". The pipe symbol is used to separate the substrings in the pattern. The resulting pattern is assigned to the variable "pattern".
- The program then splits the original string "test_str" using the regular expression pattern stored in the variable "pattern". The resulting segments are stored in the variable "res".
- Finally, the program prints the segmented string using the "print" statement and concatenating the segmented string with a string literal.
Python3
import re
# initializing string
test_str = 'geekforgeeksisbestforgeeks'
# printing original String
print("The original string is : " + str(test_str))
# initializing list words
sub_list = ["best"]
# add a '|' between the substrings to create the pattern for splitting
pattern = '|'.join(sub_list)
# split the string using the pattern
res = re.split(pattern, test_str)
# printing result
print("The segmented String : " + str(res))
OutputThe original string is : geekforgeeksisbestforgeeks
The segmented String : ['geekforgeeksis', 'forgeeks']
Time complexity: The replace() method has a time complexity of O(n), where n is the length of the string.
Auxiliary space: The original string is replaced with a new string, which has a space complexity of O(n). The segmented string is stored in a list, which has a space complexity of O(m), where m is the number of substrings. Overall, the space complexity is O(n+m).
Similar Reads
Python | Splitting string list by strings Sometimes, while working with Python strings, we might have a problem in which we need to perform a split on a string. But we can have a more complex problem of having a front and rear string and need to perform a split on them. This can be multiple pairs for split. Let's discuss certain way to solv
3 min read
Splitting String to List of Characters - Python We are given a string, and our task is to split it into a list where each element is an individual character. For example, if the input string is "hello", the output should be ['h', 'e', 'l', 'l', 'o']. Let's discuss various ways to do this in Python.Using list()The simplest way to split a string in
2 min read
Splitting String to List of Characters - Python The task of splitting a string into a list of characters in Python involves breaking down a string into its individual components, where each character becomes an element in a list. For example, given the string s = "GeeksforGeeks", the task is to split the string, resulting in a list like this: ['G
3 min read
Python | Split strings and digits from string list Sometimes, while working with String list, we can have a problem in which we need to remove the surrounding stray characters or noise from list of digits. This can be in form of Currency prefix, signs of numbers etc. Let's discuss a way in which this task can be performed. Method #1 : Using list com
5 min read
Python | Get the substring from given string using list slicing Given a string, write a Python program to get the substring from given string using list slicing. Letâs try to get this using different examples. What is substring? A substring is a portion of a string. Python offers a variety of techniques for producing substrings, as well as for determining the in
4 min read