In Python, we have several functions that can be applied to perform string operations. This tutorial shows you how to split a string by unknown number of spaces in Python. We will guide you to use the split() method to do that. Check out the following information for detailed instructions.
Split a string by unknown number of spaces in Python
To split a string by the unknown number of spaces in Python, you can apply the split() method in the following two ways:
Using the str.split() method
Call the split() method on your string object to split the string by spaces.
Syntax:
string.split(separator, maxsplit)
Parameters:
- separator: Specify the separator element. Otherwise, It defaults to a whitespace.
- maxsplit: Specify the maximum number of times to perform the split.
Note: Both of these parameters are optional.
Here is an example for you:
firstString = 'Welcome to LearnShareIT community' splitFirstString = firstString.split() print(splitFirstString)
Output:
['Welcome', 'to', 'LearnShareIT', 'community']
As a result, you can see that we have a list containing the words contained in the original string. They are separated by spaces in the original string and the split() method helps us to split them into substrings.
When you pass arguments to the split() method, you get different results. To illustrate, take a look at these examples:
Example using ‘maxsplit‘ argument:
firstString = 'Welcome to LearnShareIT community' # Perform the split only 2 times splitFirstString = firstString.split(" ",2) print(splitFirstString)
Output:
['Welcome', 'to', 'LearnShareIT community']
Example using ‘separator‘ argument:
secondString = 'Join-with-us' # Prform a split every time a '-' character is encountered splitSecondString = secondString.split('-') print(splitSecondString)
Output:
['Join', 'with', 'us']
Using the re.split() method
You can also perform splits using the split() method in combination with the regular expression. We need to use the ‘re’ module in Python with the import statement to do that.
You can see the example below:
import re firstString = 'Welcome to LearnShareIT community' # Using regular expression splitFirstString = re.split(r'\s+', firstString) print(splitFirstString)
Output:
['Welcome', 'to', 'LearnShareIT', 'community']
In this example, the symbol ‘\s‘ means a space and the accompanying ‘+‘ signifies that there can be multiple spaces. As you can see we also get the same result as in the first method.
However, with this approach you will be able to multiply empty strings like the example below:
import re firstString = ' Welcome to LearnShareIT community ' # Using regular expression splitFirstString = re.split(r'\s+', firstString) print(splitFirstString)
Output:
['', 'Welcome', 'to', 'LearnShareIT', 'community', '']
The original string has spaces at the beginning and the end, so in the output, we get two empty strings.
You can use the filter() function to remove empty elements.
import re firstString = ' Welcome to LearnShareIT community ' # Remove empty elements splitFirstString = filter(None,re.split(r'\s+', firstString)) splitFirstString = list(splitFirstString) print(splitFirstString)
Output:
['Welcome', 'to', 'LearnShareIT', 'community']
Summary
So we have introduced you to two approaches on how to split a string by unknown number of spaces in Python using the split() method. Using the first method will be more straightforward, but you will need the second method in some cases. You can choose which method to use depending on the case.
Maybe you are interested:
- Split a string only on the first Space in Python
- Split a string on uppercase letters in Python
- Split a string into words and punctuation in Python
My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.
Name of the university: HUST
Major: IT
Programming Languages: Python, JavaScript, TypeScript, Node.js, React.js