In this Python tutorial, we will learn how to split the string by space in Python. In addition, we will explore the various ways of splitting a string by space in Python.
What is a String?
Before delving into the ways of splitting strings in Python, it’s important to understand what a string is.
In Python, a string is a sequence of characters enclosed in single quotes, double quotes, or triple quotes. They are immutable sequences, meaning that once a string is created, its content cannot be changed.
Splitting a String
Python’s built-in split()
function is the most common method used to split a string. By default, this function splits a Python string into a list where each word is a list item.
Here is the syntax for the split()
function:
str.split(separator, maxsplit)
Where:
separator
is the delimiter where the string is split. If not provided, white space is the default separator.maxsplit
is an optional argument that specifies how many splits to perform. If not provided or -1, it splits at all occurrences.
Let’s look at some examples:
Example#1 Splitting a U.S. Address
Let’s say we have an address string and we want to split it into components:
address = "1600 Pennsylvania Ave NW, Washington, DC 20500"
address.split()
In this example, the split()
function divides the Python string at each space character and returns a Python list of address components.
Output:
Example#2 Splitting U.S. States from a String
Imagine you have a string of several U.S. states, and you want to split them into individual states:
states = "California Texas Florida New York Illinois"
states.split()
Here, the split()
function splits the Python string at each space character and returns a list of U.S. states.
Output:
Limiting Python Splits
If you want to limit the number of splits, you can specify the maxsplit
parameter:
Example#1 NFL Teams
teams = "Kansas City Chiefs San Francisco 49ers New England Patriots"
teams.split(' ', 3)
Here, the Python split()
function splits the string at the first three spaces, and the rest of the string is returned as the fourth element of the list.
Output:
Handling Multiple Python Spaces
The split()
function treats consecutive spaces as a single separator:
Example#1 American Novels
novels = "The Great Gatsby To Kill a Mockingbird"
novels.split()
In this example, regardless of the number of spaces between the words, the Python split()
function treats them as a single separator.
Output:
Using Python Regular Expressions for Splitting
The re
module’s split()
function can handle more complex string splitting requirements. It allows for regular expression pattern matching.
Example#1 U.S. Landmarks
import re
landmarks = "Statue of Liberty\tGrand Canyon\nMount Rushmore"
re.split('\s+', landmarks)
In this example, \s+
is a Python regular expression that matches any whitespace character (spaces, tabs, line breaks).
Output:
Conclusion
Python provides various ways to split strings, including the built-in split()
function and the re.split()
function for more complex patterns. Understanding these methods is crucial for efficient string manipulation in Python. As we’ve seen, splitting a string by space is straightforward and allows for flexibility in handling different scenarios.
- Convert string to float in Python
- Count numbers in string in Python
- Remove a character from a Python string through index
- Python Dictionary duplicate keys
- Add string to list Python
- Append to a string Python
- Python dictionary multiple values
- Get string values from list 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.