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
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
separatoris the delimiter where the string is split. If not provided, white space is the default separator.
maxsplitis 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.
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()
split() function splits the Python string at each space character and returns a list of U.S. states.
Limiting Python Splits
If you want to limit the number of splits, you can specify the
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.
Handling Multiple Python Spaces
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.
Using Python Regular Expressions for Splitting
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).
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.