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.
Summary
This is how Python split method works in practice. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
