fluttersmith/py-snippetsGitHub ↗

Split a string into words

Extract the words from text with a regex, keeping apostrophes and hyphens inside words.

Strings#textO(n) time✓ 3 examples pass on 3.11–3.14

text.split() keeps punctuation attached: 'Python!!'. A regex picks out the words alone.

words.py✓ ruff · mypy --strict
import re

DEFAULT_WORD = r"[^\W\d_]+(?:['-][^\W\d_]+)*"


def words(text: str, pattern: str = DEFAULT_WORD) -> list[str]:
    """Return the words in ``text``, as matched by ``pattern``."""
    return re.findall(pattern, text)
  1. letters, optionally joined by ' or -, so "don't" stays one word
examples✓ 3 doctests · 3.11–3.14
passed on Python 3.11 to 3.14: >>> words("I love Python!!")['I', 'love', 'Python']passed on Python 3.11 to 3.14: >>> words("don't stop-motion, café & 42")["don't", 'stop-motion', 'café']passed on Python 3.11 to 3.14: >>> words("build -q --out one-item", r"\b[a-zA-Z-]+\b")['build', 'q', 'out', 'one-item']
Edit the function or the examples and run the doctests here, on Python 3.14. Python loads only when you click (about 7 MB, cached after the first time).

How it works

  • [^\W\d_] means a letter: not a non-word character, not a digit, not an underscore. It matches é and other non-ASCII letters too.
  • (?:['-]...)* lets don't and stop-motion stay one word.
  • Pass your own pattern to change what counts as a word.

Complexity: O(n) time, O(n) space

Importfrom pysnippets.strings.words import wordsNo dependencies: copy words.py into your project, or clone the repo.

Adapted from words in 30-seconds-of-python by the 30 seconds of code contributors, CC BY 4.0. Changed: Unicode-aware default pattern that keeps apostrophes; typed. Suggest an edit.