How to split string on whitespace in Python?

Sometimes we need to split a string into individual words by separating them at whitespace characters. Python provides several methods to accomplish this task efficiently.

Using the split() Method

The simplest approach is using the built-in split() method. When called without arguments, it automatically splits on any whitespace character (spaces, tabs, newlines) and removes empty strings from the result ?

text = "Hello Everyone Welcome to Tutorialspoint"

print("The given string is:")
print(text)

print("\nThe strings after the split are:")
result = text.split()
print(result)
The given string is:
Hello Everyone Welcome to Tutorialspoint

The strings after the split are:
['Hello', 'Everyone', 'Welcome', 'to', 'Tutorialspoint']

Using re.split() Function

The re.split() function from the regular expressions module provides more control over the splitting pattern. The pattern \s+ matches one or more whitespace characters ?

import re

text = "Hello Everyone Welcome to Tutorialspoint"

print("The given string is:")
print(text)

print("\nThe strings after the split are:")
result = re.split(r'\s+', text)
print(result)
The given string is:
Hello Everyone Welcome to Tutorialspoint

The strings after the split are:
['Hello', 'Everyone', 'Welcome', 'to', 'Tutorialspoint']

Using re.findall() Function

The re.findall() method takes a different approach by finding all non-whitespace sequences. The pattern \S+ matches one or more non-whitespace characters ?

import re

text = "Hello Everyone Welcome to Tutorialspoint"

print("The given string is:")
print(text)

print("\nThe strings after the split are:")
result = re.findall(r'\S+', text)
print(result)
The given string is:
Hello Everyone Welcome to Tutorialspoint

The strings after the split are:
['Hello', 'Everyone', 'Welcome', 'to', 'Tutorialspoint']

Comparison

Method Import Required Best For
split() No Simple whitespace splitting
re.split() Yes Complex patterns or custom delimiters
re.findall() Yes Finding word patterns instead of splitting

Conclusion

Use split() for simple whitespace splitting as it's the most efficient. Use regex methods when you need more control over the splitting pattern or when dealing with complex whitespace scenarios.

Updated on: 2026-03-24T16:43:09+05:30

19K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements