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.

Topics

Learn more about Python

Course

Regular Expressions in Python

4 hr
37.3K
Learn about string manipulation and become a master at using regular expressions.
See DetailsRight Arrow
Start Course
Certification available

Course

Introduction to Python

4 hr
5.4M
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See MoreRight Arrow
Related

Mastering the Pandas .explode() Method: A Comprehensive Guide

Learn all you need to know about the pandas .explode() method, covering single and multiple columns, handling nested data, and common pitfalls with practical Python code examples.
Adel Nehme's photo

Adel Nehme

5 min

Python NaN: 4 Ways to Check for Missing Values in Python

Explore 4 ways to detect NaN values in Python, using NumPy and Pandas. Learn key differences between NaN and None to clean and analyze data efficiently.
Adel Nehme's photo

Adel Nehme

5 min

Seaborn Heatmaps: A Guide to Data Visualization

Learn how to create eye-catching Seaborn heatmaps
Joleen Bothma's photo

Joleen Bothma

9 min

Test-Driven Development in Python: A Beginner's Guide

Dive into test-driven development (TDD) with our comprehensive Python tutorial. Learn how to write robust tests before coding with practical examples.
Amina Edmunds's photo

Amina Edmunds

7 min

Exponents in Python: A Comprehensive Guide for Beginners

Master exponents in Python using various methods, from built-in functions to powerful libraries like NumPy, and leverage them in real-world scenarios to gain a deeper understanding.
Satyam Tripathi's photo

Satyam Tripathi

9 min

Python Linked Lists: Tutorial With Examples

Learn everything you need to know about linked lists: when to use them, their types, and implementation in Python.
Natassha Selvaraj's photo

Natassha Selvaraj

9 min

See MoreSee More