Python
string manipulation
whitespace
split function
programming tips

Split string on whitespace in Python

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Splitting a string on whitespace is one of the most common text operations in Python. The simplest answer is usually str.split() with no separator argument, because that version treats runs of whitespace intelligently instead of only splitting on a literal space character.

The Default split() Behavior

When you call split() with no argument, Python splits on arbitrary whitespace and collapses repeated whitespace automatically.

python
1text = "one   two\tthree\nfour"
2parts = text.split()
3
4print(parts)

Output:

text
['one', 'two', 'three', 'four']

This is usually what people mean by "split on whitespace." It handles spaces, tabs, and newline characters without leaving empty strings in the result.

That makes it better than splitting on a literal space in most normal text-processing code.

Why split(" ") Is Different

A very common mistake is passing a single space explicitly.

python
text = "one   two"
print(text.split(" "))

Output:

text
['one', '', '', 'two']

That is because split(" ") means "split only on this exact delimiter." It does not collapse repeated whitespace, and it does not treat tabs or newlines as separators.

So the rule is simple:

  • 'split() means general whitespace splitting,'
  • 'split(" ") means literal space splitting.'

For most text input, split() is the right choice.

Limiting the Number of Splits

If you only want the first few fields, use maxsplit.

python
1text = "alpha beta gamma delta"
2parts = text.split(maxsplit=2)
3
4print(parts)

Output:

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

This is useful when the string begins with structured fields but the remainder should stay together.

Empty and Whitespace-Only Strings

Another detail worth knowing is how Python handles empty input.

python
print("".split())
print("   \t\n  ".split())

Output:

text
[]
[]

That behavior is convenient because it avoids empty placeholder strings when the input contains no real tokens.

Splitting Lines from Real Text Input

A common pattern is reading lines from a file or command output and then tokenizing them by whitespace. In that situation, plain split() is still usually the best default because it naturally ignores uneven spacing.

python
line = "cpu    42   online"
columns = line.split()
print(columns)

This is one reason split() is so common in quick parsing code: it handles messy human-readable spacing with very little effort.

When Regular Expressions Make Sense

For plain whitespace tokenization, split() is enough. Regular expressions become useful only when the delimiter rule is more specific than "one or more whitespace characters."

python
1import re
2
3text = "name: mark   age: 30"
4parts = re.split(r"\s+", text)
5print(parts)

Even here, regex is not improving much. It becomes worth it only when whitespace is combined with other delimiter logic. That is why split() should remain the default choice unless the parsing rule genuinely demands more.

Common Pitfalls

  • Using split(" ") and then being surprised by empty strings in the result.
  • Forgetting that tabs and newlines also count as whitespace when using plain split().
  • Reaching for regular expressions even though the built-in split() already matches the requirement.
  • Assuming leading or trailing whitespace will produce empty elements when no separator argument is supplied.
  • Not using maxsplit when only the first few whitespace boundaries should be separated.

Summary

  • Use text.split() when you want to split on general whitespace.
  • That form handles spaces, tabs, and newlines and collapses repeated whitespace.
  • 'text.split(" ") is different because it splits only on literal spaces.'
  • 'maxsplit is useful when only the first few whitespace boundaries matter.'
  • Reach for re.split() only when the delimiter rule is more complex than ordinary whitespace.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.