string manipulation
text processing
split function
programming tutorial
code examples

Split string into words

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Splitting a string into words sounds simple until punctuation, repeated whitespace, tabs, or apostrophes show up. The correct approach depends on what you mean by "word." For basic tokenization, built-in string splitting is often enough. For more controlled text processing, regular expressions are usually the right tool.

The Simplest Case: Split on Whitespace

In Python, the most practical default is str.split() with no argument:

python
text = "alpha beta   gamma\tdelta"
words = text.split()
print(words)

Output:

python
['alpha', 'beta', 'gamma', 'delta']

This version is usually better than split(" ") because it treats runs of whitespace as one separator and handles tabs and newlines naturally.

If you write:

python
text.split(" ")

then repeated spaces produce empty strings, which is often not what you want.

Why split() and split(" ") Behave Differently

Compare the two forms:

python
1text = "one  two   three"
2
3print(text.split())
4print(text.split(" "))

Output:

python
['one', 'two', 'three']
['one', '', 'two', '', '', 'three']

That difference matters. If your goal is "words," the no-argument form is usually the correct starting point.

Handling Punctuation

Whitespace splitting alone does not remove punctuation:

python
text = "Hello, world! Let's test this."
print(text.split())

Output:

python
['Hello,', 'world!', "Let's", 'test', 'this.']

If punctuation should not be part of the tokens, use a regular expression:

python
1import re
2
3text = "Hello, world! Let's test this."
4words = re.findall(r"[A-Za-z']+", text)
5print(words)

Output:

python
['Hello', 'world', "Let's", 'test', 'this']

This pattern keeps apostrophes inside contractions while discarding commas and periods.

Choosing a Tokenization Rule

There is no universal definition of "word." Consider these cases:

  • should "can't" stay one token or become two
  • should "user_name" count as one word
  • should numbers such as 123 be included
  • should accented characters count as letters

That is why good tokenization starts with the downstream use case:

  • simple scripts may only need whitespace splitting
  • search indexing may need punctuation removal
  • natural language processing may need language-aware tokenizers

The code should reflect the actual definition you need.

Unicode and Multilingual Text

For English-only text, simple regular expressions may be enough. For multilingual text, ASCII-only patterns are often too narrow.

For example, this pattern:

python
r"[A-Za-z]+"

does not treat many non-English letters as part of words.

If you need more robust Unicode handling, a library-based tokenizer may be better than a homegrown regex. Still, for many everyday scripts, a carefully chosen regular expression is a good compromise.

Preserving Delimiters Is a Different Task

Sometimes the real requirement is not "split into words" but "split while preserving punctuation or delimiters." That needs a different approach.

Example:

python
1import re
2
3text = "Hi, there!"
4parts = re.findall(r"[A-Za-z']+|[^\w\s]", text)
5print(parts)

Output:

python
['Hi', ',', 'there', '!']

That is useful in parsers or text editors, but it is not the same as ordinary word splitting.

A Reusable Helper Function

If your project does this repeatedly, wrap the decision in a helper:

python
1import re
2
3def split_into_words(text):
4    return re.findall(r"[A-Za-z']+", text)
5
6
7print(split_into_words("Well, that's enough."))

This keeps the tokenization rule centralized instead of scattering ad hoc regexes through the codebase.

When a Real Tokenizer Is Better

If you are doing serious NLP, sentiment analysis, or language-specific work, built-in string methods are often too crude. Libraries such as spaCy, NLTK, or dedicated tokenizers understand language rules far better than split().

That does not make split() wrong. It just means that "string to words" ranges from trivial scripting to real linguistic analysis, and the right tool changes with the problem.

Common Pitfalls

The biggest mistake is using split(" ") and then being surprised by empty strings from repeated spaces.

Another issue is assuming whitespace splitting removes punctuation. It does not. "hello," and "hello" are different tokens unless you normalize them.

Developers also often write ASCII-only regexes and later discover they fail on multilingual text.

Finally, decide early whether contractions, numbers, and underscores should count as part of a word. If that rule is unclear, the tokenization code becomes inconsistent quickly.

Summary

  • For simple word splitting, text.split() is the best default.
  • Avoid split(" ") unless you specifically want empty fields from repeated spaces.
  • Use regular expressions when punctuation handling matters.
  • Tokenization rules depend on what you mean by "word."
  • For multilingual or NLP-heavy work, use a real tokenizer instead of a simplistic split rule.

Course illustration
Course illustration

All Rights Reserved.