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.
Practical example
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.
import re
text = "split this text"
words = re.split(r'\s{2,}', text)
print(words) # ['split', 'this', 'text']
Output:
['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
).