Bootcamps

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Academy

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Full-Stack Software Developer - 16w

Data Science and Machine Learning - 16 wks

Search from all Lessons


LoginGet Started

Weekly Coding Challenge

Every week, we pick a real-life project to build your portfolio and get ready for a job. All projects are built with ChatGPT as co-pilot!

Start the Challenge

Podcast: Code Sets You Free

A tech-culture podcast where you learn to fight the enemies that blocks your way to become a successful professional in tech.

Listen the podcast
← Back to How to's
Edit on Github

How to Split a String in Python?

Written by:

On this article we'll be helping you with one of the most common tasks when working with strings in Python, providing you different methods to split a string in Python. This is a quick example on how you can split a string in python by using the .split() method:

Loading...

In the previous example, we used the split() method with no parameters, to get our text split by the empty spaces into substrings.

How to split a string in Python using the split method?

Python provides a powerful method called split, which allows you to split a string into a list of substrings based on a specified separator. This method is extremely useful when you need to break down a string into its different parts.

Parameters of the split method

The split() method in Python accepts several parameters, let's explore them and see how they work.

1string.split( 2 sep=None, 3 maxsplit=-1 4)
  • sep: The sep parameter is used to specify the separator based on which the string should be split. If no separator is provided, whitespace characters (space, tab, newline, etc.) are used by default to split the string.
  • maxsplit: This parameter determines the maximum number of splits that can occur. By default, it is set to -1, which means there is no limit to the number of splits that can occur. If a positive integer is provided, the string will be split at most that many times, and the remainder of the string will be returned as the final element of the list.

Let's break down each of these parameters:

Using the split() method effectively can help you manipulate and process strings efficiently in Python.

Examples of how to use the split Method in Python

The split() method in Python is incredibly versatile, allowing you to split strings based on specified separators. Let's explore five examples showcasing its different usages:

Splitting a String by Whitespace

Loading...

In this example, the split() method is called without any arguments, which means it splits the string text into words based on whitespace. The resulting list words will contain each word from the sentence as individual elements.

Splitting a String by a Custom Separator

Loading...

Here, we split the string numbers by commas (,) using the split() method with a comma as the separator. The resulting list number_list will contain each number as an individual element.

Limiting the Number of Splits

Loading...

In this example, we split the string address by commas, but we limit the number of splits to 1. This means that the string will only be split at the first comma encountered, and the resulting list components will contain two elements: the street address and the rest of the address.

Splitting a String into Characters

Loading...

Here, we are using the split() method passing an empty string as an argument, this will split the string by each of the characters. Each character in the string becomes an element in the resulting list characters.

Splitting a Multiline String into Lines

Loading...

In this example, we split the multiline string multiline_text into individual lines using the newline character (\n) as the separator. The resulting list lines will contain each line of the original text as an element.

These examples demonstrate the various ways you can leverage the split() method in Python to manipulate strings and extract desired information.

In this article, we've covered various methods to split a string into substrings in Python, an essential skill for effective data processing and manipulation. By exploring practical examples and best practices, you're better equipped to handle diverse string manipulation tasks in your Python projects. For more insights into Python programming and data processing, and to further enhance your skills, consider signing up for free on our Start Coding using Python course at 4Geeks.com.