Python | Ways to split a string in different ways
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
Method 2: Using Itertools
Method#3 : Using re module and string slicing
['Geeks', 'Geeks_for', 'Geeks_for_geeks', 'Geeks_for_geeks_is', 'Geeks_for_geeks_is_best']