Sunday, November 17, 2024
Google search engine
HomeLanguagesPython program to split a string by the given list of strings

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

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

Input : test_str = ‘geekforneveropenbestforneveropen’, sub_list = [“best”] 
Output : [‘geekforneveropen’, ‘best’, ‘forneveropen’] 
Explanation : “best” is extracted as different list element.

Input : test_str = ‘geekforneveropenbestforneveropenCS’, sub_list = [“best”, “CS”] 
Output : [‘geekforneveropen’, ‘best’, ‘forneveropen’, “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 = 'geekforneveropenisbestforneveropen'
 
# 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))


Output

The original string is : geekforneveropenisbestforneveropen
The segmented String : ['geekforneveropenis', 'best', 'forneveropen']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method : Using replace(),split() methods

Approach

  1. Initiate a for loop to traverse list of strings
  2. Replace the string in original string by appending “*” front and back
  3. Finally split the string with * as parameter
  4. This will return a list, display the list

Python3




# Python3 code to demonstrate working of
# Separate specific Strings
 
# initializing string
test_str = 'geekforneveropenisbestforneveropen'
 
# 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))


Output

The original string is : geekforneveropenisbestforneveropen
The segmented String : ['geekforneveropenis', 'best', 'forneveropen']

Time Complexity : O(N)
Auxiliary Space : O(N)

Method 3 :  Use the re module

 step by step approach :

  1. The program starts by importing the “re” module. This module provides regular expression operations in Python.
  2. The program then initializes a string variable named “test_str” with the value “geekforneveropenisbestforneveropen”.
  3. The program then prints the original string using the “print” statement and concatenating the original string with a string literal.
  4. The program initializes a list variable named “sub_list” with the value [“best”].
  5. 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”.
  6. 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”.
  7. 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 = 'geekforneveropenisbestforneveropen'
 
# 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))


Output

The original string is : geekforneveropenisbestforneveropen
The segmented String : ['geekforneveropenis', 'forneveropen']

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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments