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.
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.
Output:
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.
Output:
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.
Output:
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.
Output:
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.
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."
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
maxsplitwhen 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.' - '
maxsplitis 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
- Split string with multiple delimiters in Python
- Splitting a list into N parts of approximately equal length
- Splitting on last delimiter in Python string?
- SQLAlchemy - Getting a list of tables
- SQLAlchemy create_all does not create tables
- SQLAlchemy default DateTime
- SQLAlchemy IN clause
- SQLAlchemy ORDER BY DESCENDING?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.