Python | Split String Space

5/5 - (1 vote)

⭐Summary: Use "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.

Minimal Example

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 Formulation

πŸ“œ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()

Python’s built-in 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.

Code:

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.

Code:

import re
text = "Python C Java C++"
print(re.split(' ', text))

# ['Python', 'C', 'Java', 'C++']

πŸ†Related Read: Python Regex Split

Method 3: Using filter()

Python’s built-in 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.

Code:

text = "Python C Java C++"
print(list(filter(None, text.split())))

# ['Python', 'C', 'Java', 'C++']

πŸ†Related Read: Python filter()

Method 4: Using re.findall()

The 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.

Code:

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).

Conclusion

Hurrah! We have successfully solved the given problem using as many as four different ways. I hope this article helped you. Please subscribe and stay tuned for more interesting articles!

Related Reads:
πŸ† Python | Split String Variable Spaces
πŸ† Python | Split String by Comma and Whitespace
πŸ† Python | Split String by Whitespace
πŸ† Python | Split String and Keep Whitespace


Python Regex Course

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: