EN
Python - split string with more than 1 space between words
0 points
In this article, we would like to show you how to split a string with more than 1 space between words in Python.
In this example, we import re
module for regular expression (regex), so we can use re.split()
method to split the text
string with two or more white spaces between words.
xxxxxxxxxx
1
import re
2
3
text = "split this text"
4
5
words = re.split(r'\s{2,}', text)
6
7
print(words) # ['split', 'this', 'text']
Output:
xxxxxxxxxx
1
['split', 'this', 'text']
Note:
\s
- matches any whitespace character (spaces, tabs, line breaks),{2,}
- matches two or more of the preceding token (in our case two or more\s
).