Skip to main content
HomeAbout PythonLearn Python

String Split in Python Tutorial

Jun 2020  · 2 min read

Python string method split() inputs a string value and outputs a list of words contained within the string by separating or splitting the words on all the whitespaces by default. It also has an optional argument for limiting the number of splits.

The split() method converts the string into other data types like lists by breaking a string into pieces. Python provides us with two methods: .split() and .rsplit().

Both .split(), and .rsplit() take a separating element by which we are splitting the string, and a maxsplit tells us the maximum number of substrings we want.

Example of Using the .split() Method

string1

Let's split the above string using the .split() method. Notice that the .split() method starts splitting at the left of the string. It takes two arguments: separator sep and maximum splits of the string maxsplit.

string2

Example of Splitting Strings into Many Substrings

In the below example, you will split the string file into many substrings at line boundaries. Then, you will print out the resulting variable file_split. Finally, complete the for-loop to split the strings into many substrings using commas as a separator element.

substrings

When we run the above code, it produces the following result:

['mtv films election, a high school comedy, is a current example', 'from there, director steven spielberg wastes no time, taking us into the water on a midnight swim'] ['mtv films election', ' a high school comedy', ' is a current example'] ['from there', ' director steven spielberg wastes no time', ' taking us into the water on a midnight swim'].

Try it for yourself.

To learn more about string operations, please see this video from our course, Regular Expressions in Python and check out our Python Regular Expression Tutorial.

This content is taken from DataCamp’s Regular Expressions in Python course by Maria Eugenia Inzaugarat.

Learn more about Python

Regular Expressions in Python

Beginner
4 hr
32.7K
Learn about string manipulation and become a master at using regular expressions.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

Pandas 2.0: What’s New and Top Tips

Dive into pandas 2.0, the latest update of the essential data analysis library, with new features like PyArrow integration, nullable data types, and non-nanosecond datetime resolution for better performance and efficiency.
Moez Ali's photo

Moez Ali

9 min

PyTorch 2.0 is Here: Everything We Know

Explore the latest release of PyTorch, which is faster, more Pythonic, and more dynamic.
Abid Ali Awan's photo

Abid Ali Awan

6 min

An Introduction to Python T-Tests

Learn how to perform t-tests in Python with this tutorial. Understand the different types of t-tests - one-sample test, two-sample test, paired t-test, and Welch’s test, and when to use them.
Vidhi Chugh's photo

Vidhi Chugh

13 min

Matplotlib time series line plot

This tutorial explores how to create and customize time series line plots in matplotlib.
Elena Kosourova's photo

Elena Kosourova

8 min

Step-by-Step Guide to Making Map in Python using Plotly Library

Make your data stand out with stunning maps created with Plotly in Python
Moez Ali's photo

Moez Ali

7 min

High Performance Data Manipulation in Python: pandas 2.0 vs. polars

Discover the main differences between Python’s pandas and polars libraries for data science
Javier Canales Luna's photo

Javier Canales Luna

16 min

See MoreSee More