"given string".split(' ') to split the given string by space and store each word as an individual item in a list. Some other ways to split using space include using a list comprehension and the regex library.
text = "abc xyz lmn pqr" # Method 1 print(text.split()) # Method 2 import re print(re.split(' ', text)) # Method 3 print(list(filter(None, text.split()))) # Method 4 print([x for x in re.findall(r'\S+', text) if x != '']) # OUTPUT: ['abc', 'xyz', 'lmn', 'pqr']
📜Problem: Given a string, how will you split the string into a list of words using space as a delimiter?
Example: Let’s understand the problem with the help of an example.
# Input: text = "Python C Java C++" # Output: ['Python', 'C', 'Java', 'C++']
Now without any further ado, let’s dive into the numerous ways of solving this problem.
Method 1: Using split()
split() function splits the string at a given separator and returns a split list of substrings. Here’s how the
split() function works:
'finxterx42'.split('x') will split the string with the character ‘
x’ as the delimiter and return the following list as an output:
['fin', 'ter', '42'].
Approach: To split a string by space, you can simply use the
split() function directly without specifying any separator. If no separator is specified, the
split() function uses whitespaces as separator by default.
text = "Python C Java C++" print(text.split()) # ['Python', 'C', 'Java', 'C++']
🏆Related Read: Python String split()
Method 2: Using re.split()
Another way of separating a string by using the underscore as a separator is to use the
re.split() method from the regex library. The
re.split(pattern, string) method matches all occurrences of the pattern in the string and divides the string along the matches resulting in a list of strings between the matches. For example,
re.split('a', 'bbabbbab') results in the list of strings
['bb', 'bbb', 'b'].
Approach: You can use the
re.split() method as
re.split(' ', text) where
' ' returns a match whenever the string contains a space. Whenever any space is encountered, the string gets separated and the split substring gets stored as an element within the resultant list.
import re text = "Python C Java C++" print(re.split(' ', text)) # ['Python', 'C', 'Java', 'C++']
🏆Related Read: Python Regex Split
Method 3: Using filter()
filter() function filters out the elements that pass a filtering condition. It takes two arguments: (i) function and (ii) iterable. The function assigns a Boolean value to each element in the iterable to check whether the element will pass the filter or not. It returns an iterator with the elements that passes the filtering condition.
Approach: Use the
filter() method to split the string by space. The function takes
None as the first argument and the list of split strings as the second argument. The
filter() function then iterates through the list and removes any empty elements. As the
filter() method returns an iterator object, we need to use the
list() constructor to convert the object into a list.
text = "Python C Java C++" print(list(filter(None, text.split()))) # ['Python', 'C', 'Java', 'C++']
🏆Related Read: Python filter()
Method 4: Using re.findall()
re.findall(pattern, string) method scans the string from left to right, searching for all non-overlapping matches of the pattern. It returns a list of strings in the matching order- when scanning the string from left to right.
Approach: You can use the
re.findall() method from the regex module to split the string by space. Use ‘
\S+‘ as the pattern that can be fed into the
findall function to solve the problem. It simply means all the characters except space will be grouped together.
import re text = "Python C Java C++" print([x for x in re.findall(r'\S+', text) if x != '']) # ['Python', 'C', 'Java', 'C++']
🏆Related Read: Python re.findall() – Everything You Need to Know
Google, Facebook, and Amazon engineers are regular expression masters. If you want to become one as well, check out our new book: The Smartest Way to Learn Python Regex (Amazon Kindle/Print, opens in new tab).
Google engineers are regular expression masters. The Google search engine is a massive text-processing engine that extracts value from trillions of webpages.
Facebook engineers are regular expression masters. Social networks like Facebook, WhatsApp, and Instagram connect humans via text messages.
Amazon engineers are regular expression masters. Ecommerce giants ship products based on textual product descriptions. Regular expressions rule the game when text processing meets computer science.
If you want to become a regular expression master too, check out the most comprehensive Python regex course on the planet: