Splitting a string into a list is a common operation in Python. For instance, you may have a list of ingredients in a recipe that you want to split up into a list.
That’s where the Python string
split() method comes in. The
split() method allows you to break up a string into a list of substrings, based on the separator you specify.
This tutorial will discuss, with examples, the basics of the Python
split() function, and how you can use it to break up strings in Python. By the end of reading this tutorial, you’ll be a master at using the
split() string function.
Strings are lists of characters in Python and allow you to store text-based data.
Here is an example of a string in Python:
example_string = "I am an example string!"
In this code, we assign the string “I am an example string!” to the variable
example_string. Because this string is just a list of characters, we can use a number of Python string methods to break it up into smaller components.
split() is one example of a string method that allows us to divide our string. Let’s explore how this method works.
Python String split()
split() Python method allows you to break up a string based on a separator value.
The syntax for the
split() method is as follows:
split() method is appended at the end of a string, and accepts two parameters:
- separator is the character the
split()function will use to separate out your string. By default, any whitespace character will be used as a separator (space, newline). This parameter is optional.
- maxsplit defines the maximum number of splits that should occur. By default, maxsplit is equal to -1, which means an infinite number of splits will run. This parameter is also optional.
Let’s walk through a few examples of the Python
split() function to demonstrate how it works.
Python Split Examples
Suppose you have a string of ingredients for a chocolate chip muffin that you want to divide into a list. Each ingredient is separated by a comma in your string. By using the
split() method, you can divide the string of ingredients up into a list.
Here’s the code we would use to divide our ingredients string into a list:
ingredients = "2 eggs, 125ml Vegetable oil, 250ml Semi-skimmed milk, 250g golden caster sugar, 400g self-raising flour, 100g chocolate chips" list_of_ingredients = ingredients.split(",") print(list_of_ingredients)
Our code returns:
['2 eggs', ' 125ml Vegetable oil', ' 250ml Semi-skimmed milk', ' 250g golden caster sugar', ' 400g self-raising flour', ' 100g chocolate chips']
Let’s break down our code. First, we define a variable called
ingredients which stores our list of ingredients. Each ingredient in our list is separated using a comma (,).
Next, we use the
split() method to split up our
ingredients string using commas. Then, we assign the result of the
split() method to the variable
Finally, we print out the variable
list_of_ingredients to the console. As you can see, our code has turned our string into a list with six values: one for each ingredient.
In addition, we can use the
maxsplit parameter to limit how many times our string should be split. Suppose we only want to split up the first three ingredients in our list. We could do so using this code:
ingredients = "2 eggs, 125ml Vegetable oil, 250ml Semi-skimmed milk, 250g golden caster sugar, 400g self-raising flour, 100g chocolate chips" list_of_ingredients = ingredients.split(",", 3) print(list_of_ingredients)
Our code returns:
['Two eggs', ' 125ml Vegetable oil', ' 250ml Semi-skimmed milk', ' 250g golden caster sugar, 400g self-raising flour, 100g chocolate chips']
This code is almost the same as our last example, but with one difference: we have set the value of the
maxsplit parameter to 3. This means that our
split() function will split our string up using the character we specify (in this case, a comma), for a maximum of three times. Then, the rest of the string will be added to the end of our list.
As you can see, our list now only has four items. The first three items are individual ingredients, and the last item in our list contains the rest of the items from our original string.
The Python string
split() method allows you to divide a string into a list at a specified separator. For instance, you could use
split() to divide a string by commas (,), or by the letter
This tutorial discussed the basics of strings in Python and how to use the string
split() method. Now you’re ready to start using the
split() method like a Python professional!