Skip to main content
HomeAbout PythonLearn Python

String Split in Python Tutorial

Learn how you can perform various operations on string using built-in Python functions like split, join and regular expressions.
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

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

10 Essential Python Skills All Data Scientists Should Master

All data scientists need expertise in Python, but which skills are the most important for them to master? Find out the ten most vital Python skills in the latest rundown.

Thaylise Nakamoto

9 min

The 7 Best Python Certifications For All Levels

Find out whether a Python certification is right for you, what the best options are, and the alternatives on offer in this comprehensive guide.
Matt Crabtree's photo

Matt Crabtree

18 min

A Complete Guide to Socket Programming in Python

Learn the fundamentals of socket programming in Python
Serhii Orlivskyi's photo

Serhii Orlivskyi

41 min

Textacy: An Introduction to Text Data Cleaning and Normalization in Python

Discover how Textacy, a Python library, simplifies text data preprocessing for machine learning. Learn about its unique features like character normalization and data masking, and see how it compares to other libraries like NLTK and spaCy.

Mustafa El-Dalil

5 min

Coding Best Practices and Guidelines for Better Code

Learn coding best practices to improve your programming skills. Explore coding guidelines for collaboration, code structure, efficiency, and more.
Amberle McKee's photo

Amberle McKee

26 min

Pandas Profiling (ydata-profiling) in Python: A Guide for Beginners

Learn how to use the ydata-profiling library in Python to generate detailed reports for datasets with many features.
Satyam Tripathi's photo

Satyam Tripathi

9 min

See MoreSee More