While working with Python, we can have problem in which we need to perform the task of splitting the words of string on spaces. But sometimes, we can have comma separated words, which have comma’s joined to words and require to split them separately. Lets discuss certain ways in which this task can be performed.
Method #1 : Using replace()
Using replace() is one way to solve this problem. In this, we just separate the joined comma from string to spaced so that they can be splitted along with other words correctly.
Python3
# Python3 code to demonstrate working of # Custom Split Comma Separated Words # Using replace() # initializing string test_str = 'neveropen, is, best, for, Lazyroar' # printing original string print ( "The original string is : " + str (test_str)) # Distance between occurrences # Using replace() res = test_str.replace( ", " , " , " ).split() # printing result print ( "The strings after performing splits : " + str (res)) |
The original string is : neveropen, is, best, for, Lazyroar The strings after performing splits : ['neveropen', ',', 'is', ',', 'best', ',', 'for', ',', 'Lazyroar']
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string.
Method #2 : Using re.findall()
This problem can also be used using regex. In this, we find the occurrences of non space word and perform a split on that basis.
Python3
# Python3 code to demonstrate working of # Custom Split Comma Separated Words # Using re.findall() import re # initializing string test_str = 'neveropen, is, best, for, Lazyroar' # printing original string print ( "The original string is : " + str (test_str)) # Distance between occurrences # Using re.findall() res = re.findall(r '\w+|\S' , test_str) # printing result print ( "The strings after performing splits : " + str (res)) |
The original string is : neveropen, is, best, for, Lazyroar The strings after performing splits : ['neveropen', ',', 'is', ',', 'best', ',', 'for', ',', 'Lazyroar']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3 – Custom Split Comma Separated Words Using String split() Method.
In this method to achieve the same result is by using the string split() method. we can split the string based on the comma separator, and then strip any whitespace characters from each resulting substring.
Python3
test_str = 'neveropen, is, best, for, Lazyroar' # Split the string based on commas and strip whitespace from each substring res = [s.strip() for s in test_str.split( ',' )] # printing result print ( "The strings after performing splits : " + str (res)) |
The strings after performing splits : ['neveropen', 'is', 'best', 'for', 'Lazyroar']
Time complexity: O(n), where n is the length of the input string test_str.
Auxiliary space: O(n), where n is the length of the input string test_str.
Method #4: Using list comprehension
This program splits a string into words using the comma as a delimiter, and then removes any leading or trailing whitespace from each word using a list comprehension. The resulting list of stripped words is then printed.
Python3
test_str = 'neveropen, is, best, for, Lazyroar' res = [word.strip() for word in test_str.split( ',' )] print (res) |
['neveropen', 'is', 'best', 'for', 'Lazyroar']
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string.
Method #5: Using the string.split() method
Python3
test_str = 'neveropen, is, best, for, Lazyroar' # Split words with list comprehension res = [word.strip() for word in test_str.split( "," )] # printing result print ( "The strings after performing splits : " + str (res)) |
The strings after performing splits : ['neveropen', 'is', 'best', 'for', 'Lazyroar']
Time complexity: O(n), where n is the length of the input string, test_str.
Auxiliary space: O(n), where n is the length of the input string, test_str.
Method #6: Using the re.split() method from the re module
One additional method for splitting comma-separated words in Python is using the re.split() method from the re module.
Step-by-step approach:
- Import the re module.
- Initialize the string to be split, test_str.
Define a regular expression pattern that matches commas followed by any number of spaces (“,\s*”). - Use the re.split() method with the regular expression pattern to split the string.
- Print the resulting list of split strings.
Below is the implementation of the above approach:
Python3
import re # initializing string test_str = 'neveropen, is, best, for, Lazyroar' # printing original string print ( "The original string is : " + str (test_str)) # Custom Split Comma Separated Words Using re.split() pattern = ",\s*" res = re.split(pattern, test_str) # printing result print ( "The strings after performing splits : " + str (res)) |
The original string is : neveropen, is, best, for, Lazyroar The strings after performing splits : ['neveropen', 'is', 'best', 'for', 'Lazyroar']
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string. In the best case, where the input string contains only one comma, the space complexity is O(1).