Languages
[Edit]
EN

Python - split string with more than 1 space between words

0 points
Created by:
eyllanesc
517

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

See also

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Python - string (popular problems)

Python - split string with more than 1 space between words
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join