Open In App

Python – Selectively Split in Strings

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python strings, we may have to perform a split. Not sometimes, normal one, depending on deliminator but something depending upon programming constructs like elements, numbers, words etc and segregate them. Lets discuss a way in which this task can be solved. 

Method : Using re.findall() A specific regex can be employed to perform this task. In this we construct regex using different elements like numbers, words punctuations etc. 

Python3




# Python3 code to demonstrate working of
# Selective Split in Strings
# Using regex
import re
 
# initializing string
test_str = "print(\"geeks\");"
 
# printing original string
print("The original string is : " + test_str)
 
# Selective Split in Strings
# Using regex
res = re.findall('\d+\.\d+|\d+|\w+|[^a-zA-Z\s]', test_str)
 
# printing result
print("The splitted string is : " + str(res))


Output

The original string is : print("geeks");
The splitted string is : ['print', '(', '"', 'geeks', '"', ')', ';']

Time Complexity: O(n)

Space Complexity: O(n)

Approach#2:  Using a loop

this approach involves iterating over each character in the input string and building substrings based on the separators (“, (, ), ;). The resulting substrings and separators are stored in a list and returned.

Algorithm

1. Define a list to store the resulting substrings and separators.
2. Define a variable to store the current substring being built.
3. Iterate over each character in the input string.
4. If the current character is a separator, add the current substring to the list and add the separator to the list.
5. Otherwise, append the current character to the current substring.
6. Add the final substring to the list.
7. Return the resulting list of substrings and separators.

Python3




def split_string(string):
    substrings = []
    current_substring = ''
    for char in string:
        if char in ['"', '(', ')', ';']:
            if current_substring != '':
                substrings.append(current_substring)
                current_substring = ''
            substrings.append(char)
        else:
            current_substring += char
    if current_substring != '':
        substrings.append(current_substring)
    return substrings
 
string = 'print("geeks");'
print(split_string(string))


Output

['print', '(', '"', 'geeks', '"', ')', ';']

Time Complexity: O(n), where n is the length of the input string, because we iterate over each character in the string once to split it.
Auxiliary Space: O(n), where n is the length of the input string, because we create a new list to store the resulting substrings and separators.

METHOD 3: Using re.split module

APPROACH:

In this approach, we use a regular expression to split the string based on either a delimiter ((, ), “, or 😉 or a sequence of one or more word characters (\w+). The regular expression is enclosed in parentheses to capture the delimiters as separate items in the resulting list. The filter() function is used to remove the empty.

ALGORITHM:

Python3




import re
 
string = 'print("geeks");'
 
split_string = re.split(r'([\(\)"\;])|(\w+)', string)
 
# Removing the empty strings and None values from the list
split_string = list(filter(lambda x: x != '' and x is not None, split_string))
 
print(split_string)  # ['print', '(', '"', 'geeks', '"', ')', ';']


Output

['print', '(', '"', 'geeks', '"', ')', ';']

The time complexity of this approach is O(n)

The auxiliary space of this approach is O(n).



Last Updated : 15 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads