The Wayback Machine - https://web.archive.org/web/20240118173828/https://www.geeksforgeeks.org/python-ways-to-split-a-string-in-different-ways/
Open In App
Related Articles

Python | Ways to split a string in different ways

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The most common problem we have encountered in Python is splitting a string by a delimiter, But in some cases we have to split in different ways to get the answer. In this article, we will get substrings obtained by splitting string in different ways. Examples:

Input : Paras_Jain_Moengage_best 

Output : [‘Paras’, ‘Paras_Jain’, ‘Paras_Jain_Moengage’, ‘Paras_Jain_Moengage_best’] 

Input : chunky_2808_GFG_Codechef 

Output : [‘chunky’, ‘chunky_2808’, ‘chunky_2808_GFG’, ‘chunky_2808_GFG_Codechef’]

Below are some ways to do the task.

Method #1: Using Iteration 

Python3




# Python code to split string in substring manner
 
# Input initialisation
Input = "Geeks_for_geeks_is_best"
 
# Split initialise
split_string = Input.split('_')
 
# Output list initialise
Output = []
 
# Iteration
for a in range(len(split_string)):
    temp = split_string[:a + 1]
    temp = "_".join(temp)
    Output.append(temp)
 
# print output
print(Output)


Output:[‘Geeks’, ‘Geeks_for’, ‘Geeks_for_geeks’, ‘Geeks_for_geeks_is’, ‘Geeks_for_geeks_is_best’]

Time Complexity: O(n), where n is the length of the string.

Space Complexity: O(n), where n is the length of the string.

Method 2: Using Itertools 

Python3




# Python code to split string in substring manner
 
# Importing
from itertools import accumulate
 
# Input initialisation
Input = "Geeks_for_geeks_is_best"
 
# Using accumulate
Output = [*accumulate(Input.split('_'), lambda temp1, temp2 :
                                   '_'.join([temp1, temp2])), ]
 
# Printing output
print(Output)


Output:[‘Geeks’, ‘Geeks_for’, ‘Geeks_for_geeks’, ‘Geeks_for_geeks_is’, ‘Geeks_for_geeks_is_best’]

Time complexity: O(n), where n is the length of the input string.

Auxiliary space: O(n), as we are creating a list to store the output of the accumulate function.

Method#3 : Using re module and string slicing

Python3




# Python code to split string in substring manner
# Importing
import re
 
# Input initialisation
Input = "Geeks_for_geeks_is_best"
 
# Using re module with string slicing to generate substring
ans = []
for i in re.finditer("(_)", Input):
    temp = Input[:i.span()[0]]
    ans.append(temp)
     
# inserting last substring
ans.append(Input)
 
# Printing output
print(ans)


Output:

['Geeks', 'Geeks_for', 'Geeks_for_geeks', 'Geeks_for_geeks_is', 'Geeks_for_geeks_is_best']

Time complexity: O(n), where n is the length of the input string.

Auxiliary space: O(m), where m is the number of substrings generated. 


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 26 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials