What is a Python split method?
Split is a string method that is used to convert a string to a list based on a separator that we choose and the number of splits that we make. Separators are also known as delimiters and can be spaces, commas, hashtags etc.
Example one – basics of the Python Split method
In this example, we are going to create a string of animals that contains the following entries: animals = ‘horse, chicken, pig, goat, donkey, cow’. We can verify that this is a string by printing out the type of the string animals and we will get that this is a class of string.
Now, what we would quite possibly want to do is we would want to turn this into a list of the individual items. Now the best way to do that is to use the Python split command. We will first begin by declaring the name of the list that we intend to create and then proceed to write “animals.split()”. So the first thing and the most important one and the one that we use the most is the delimiter. The delimiter argument basically states what we want to split the string based on.
Now, fortunately, our string here looks like it should be a list already and we have these commas that we can use. So inside that parentheses, we are going to put a comma. Now, this is going to tell python to split this string up on this comma.
So if we print “new” and print out its type as well we will obtain a list of elements in the animal string and we will also see that “new ” is of class type list. As shown in the code below.
Example two – using Python Split on lists
In this example, we are going to illustrate how to split a sentence into a list. We will begin by creating the sentence = “cat sat on the mats”. If we print out the type of this sentence, it is going to tell us again that this is a string.
We can proceed and split up the sentence by saying that new = sentence.split(). If we leave it like that, it is going to automatically split it based on the space for us. As shown below.
Now we can get well, much more creative with this, and say let’s split the sentence up based on the letter ‘e’ as the delimiter. To do that we have to pass in letter ‘e’ within the parentheses that come after the Python split method as shown below.
In the output above we can see that it has run through our sentence and it has split the sentence based on the letter ‘e’ besides that, it has also been removed.
Example three – other methods based on Python Split
We can also pass in another parameter known as “maxsplit”. When used together the Python split method is going to start at the beginning of the string and it’s going to split it up based on the delimiter that we choose as per normal as we just looked at and it’s also going to do the number of splits that we tell it to and then it’s going to stop
We can now just pass in our delimiter as space and then also pass in maxsplit is equal to and let’s give it a number of one. So this is going to do one split and then stop.
So if we run this we can see that it has split out the first word there because it’s found the space that we want to split it on. And science we’ve told the method that we only want to do one split so the rest of the string stays as it was.
We can also change the number of splits that we want to do, we are going to change maxsplit to three. This is going to split the string into a list with three first separate words and then the rest will remain intact as shown below.
We can also use another inbuilt method known as “splitlines” to split up a text on a new line character. We could do that normally with a Python split as well. We are passing the new line character, this is just an easier way to do it. It’s built-in so why wouldn’t we use it?
To illustrate this let’s create a new sentence and call it lines = “one day \n i was walking in the woods”.In python, a new line is depicted by a backslash and an n. Now if we print outlines we will notice that “one day” and “i was walking in the woods” are on separate lines as shown below.
Now, what we can do is we can actually split this using dot split lines, which makes our lives really easy splitlines and when I print that out, where are given a list with the sentences split out on the line break as shown here.
In our Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
- How to create perfect HTML tables?
- HTML color codes
- CSS background images
- Best Programming Books You Must Read in 2021
- Python for loop
- Creating a table using HTML and CSS
- Best way of using Java Arrays and ArrayLists
- Best way of using Python Sets
- Best ways of using a Python Dictionary
- Best way of using Python Classes
- Best way of using Python Range
- Best way of using Python if-else
- Best way of using Python RegEx
- Best way of using Python Lists
- Best way of using Python Enumerate
- Best way of using Python Functions
- Python Split
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.