As a data scientist working with text data as a part of my project, I recently encountered a challenge where I needed to process raw text data from customer feedback which needed splitting the strings by whitespace. After researching different methods, I discovered the most efficient ways to achieve this in Python. In this tutorial, I will explain how to split string by whitespace in Python.
Split String by Whitespace in Python Using split() Method
The split() method in Python is a built-in function that divides a string into a list based on a specified delimiter. By default, this delimiter is any whitespace character (spaces, tabs, and newlines). This makes split() incredibly versatile for text-processing tasks.
Let us see some scenarios where we can make use of the split() method in Python.
Read How to Get the Last 3 Characters of a String in Python?
1. Basic Use of split() Method in Python
Let’s start with a simple example. Suppose you have a string representing a customer’s address:
address = "123 Main St New York NY 10001"Using the split() method, you can easily break this string into individual components:
address_parts = address.split()
print(address_parts)This will output:
['123', 'Main', 'St', 'New', 'York', 'NY', '10001']I have executed the above example code and added the screenshot below.

As you can see, the split() method has divided the string at each whitespace character and returned a list of substrings.
Check out How to Get the Last 4 Characters of a String in Python?
2. Handle Multiple Whitespace Characters
In real-world scenarios, Python strings may contain multiple spaces, tabs, or newlines. The split() method handles these gracefully by treating consecutive whitespace characters as a single delimiter.
Consider the following example:
address = "123 Main St\tNew York\nNY 10001"
address_parts = address.split()
print(address_parts)This will output:
['123', 'Main', 'St', 'New', 'York', 'NY', '10001']I have executed the above example code and added the screenshot below.

Despite the varied whitespace characters, the split() method correctly splits the string into its components.
Read How to Truncate a String to a Specific Length in Python?
3. Specify a Maximum Split
Sometimes, you may only want to split a string a certain number of times. The split() method in Python allows you to specify a parameter maxsplit , which limits the number of splits performed.
For example, if you only want to split the address string into two parts:
address = "123 Main St New York NY 10001"
address_parts = address.split(maxsplit=2)
print(address_parts)This will output:
['123', 'Main', 'St New York NY 10001']I have executed the above example code and added the screenshot below.

Here, the string is split into three parts, with the last part containing the remaining text.
Check out How to Print Strings and Integers Together in Python?
4. Split Strings with Regular Expressions
In some cases, you may need more control over how a string is split. Python’s re module provides the re.split() function, which allows you to split a string using regular expressions.
For example, to split a string and include the whitespace characters in the result:
import re
address = "123 Main St\tNew York\nNY 10001"
address_parts = re.split(r'(\s+)', address)
print(address_parts)This will output:
['123', ' ', 'Main', ' ', 'St', '\t', 'New', ' ', 'York', '\n', 'NY', ' ', '10001']The re.split() function splits the string at each sequence of whitespace characters and includes them in the result.
Read How to Convert a String to Date in Python?
Example: Process Customer Addresses
Let’s consider a practical example where we process a list of customer addresses from a text file. Each line in the file contains an address, and we need to extract the city and state.
addresses = [
"123 Main St New York NY 10001",
"456 Elm St Los Angeles CA 90001",
"789 Pine St Chicago IL 60601"
]
for address in addresses:
parts = address.split()
city = parts[-3]
state = parts[-2]
print(f"City: {city}, State: {state}")This will output:
City: New, State: York
City: Los, State: Angeles
City: Chicago, State: ILBy splitting the address string and accessing the appropriate parts, we can easily extract the city and state information.
Check out How to Find the Second Occurrence of a Substring in a Python String?
Conclusion
In this tutorial, I have explained how to split string by whitespace in Python. I discussed the split() method and by using the split() method how to handle multiple whitespace characters, specify a maximum split, split string with regular expressions. I also discussed a practical example.
You may also like to read:
- How to Do Case-Insensitive String Comparisons in Python?
- How to Use Python Triple Quotes with F-Strings for Multiline Strings?
- How to Fix Unterminated String Literals in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.