Python split string by space [With Examples]

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:

Python split string by space
Python split string by space

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:

Python split string by space example
Python split string by space example

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:

How to split string by space using python
Split string by space using Python

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:

How to split string by space using python example
Split string by space using python example

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:

How to split string by space in python
Split string by space in Python

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.