Python provides a built-in method for splitting strings. With the split() function, we can break a single string into a list of strings.
Using split() is a simple and efficient method for breaking down large strings into more manageable parts.
This can be a useful method if we’re tasked with working on text files, as we’ll see later on.
Definition and Syntax of the Split Function
The split() method separates a string into individual words using a delimiter, also referred to as the separator. Python uses whitespace as the default separator, but you are free to provide an alternative.
The separator can be anything, but it’s generally a character used to separate the words in a string. For example, a comma is often used to break up a list.
# the following string uses a comma as the separator.
groceries = “Bread,Milk,Eggs,Bananas,Coffee”
The split() function will return a list of strings. By default there is no limit to how long the returned list can be. But you can change this with the maxsplit setting.
Syntax:
my_string.split(separator,maxsplit)
separator: This is the delimiter Python will use to split the string. Whitespace is used as the separator if one is not provided.
maxsplit: This setting is used to determine how many times the string should be split. This list has no limit by default.
Why Use the split() Function in Python?
There are plenty of good reasons to use the split() function. Whether you’re working with a comma separated value (CSV) file, or simply trying to break down a large string into smaller pieces, split() offers an efficient means of splitting strings in Python.
The split() function will break a string into list of words. These are given back to us in the form of a Python string array.
Here’s a step-by-step guide to using split():
- Create a string.
- Use the Python split() function
- Print the result
gandalf = "You shall not pass."
gandalf.split() # The string will be split based on whitespace.
Here’s what it looks like when we split the string.
['You', 'shall', 'not', 'pass.']
Use the split() function with python string concatenation to quickly join and split strings. The split() function is also great for quickly parsing comma separated value (CSV) files.
Examples of the Python Split Function
When we use the split() function to split a string, Python returns a new string array. This array holds the split string.
For example. If we have a string that looks like this:
frosty_string = "Some say the world will end in fire."
We can see that the words of the string are separated by whitespace. Running split() with this string would return a new array containing the words of the opening line of a famous poem by Robert Frost.
Example 1: Using Python split() With a Basic String
# this line is from Fire and Ice by Robert Frost
frosty_string = "Some say the world will end in fire."
words = frosty_string.split()
print(words)
Output
['Some', 'say', 'the', 'world', 'will', 'end', 'in', 'fire.']
It’s also possible to use split() to break a list into a predefined number of parts. As we saw in its definition, the split() function takes an optional second argument. By setting maxpslit, we can tell Python to break the list into two pieces.
Example 2: Breaking a List Into Two Parts Using Python Split
fruit = "Apples Oranges"
# setting maxsplit to 1 will return a list with 2 parts
two_max = fruit.split(' ', 1)
print(two_max)
Output
['Apples', 'Oranges']
Using Commas to Split String Data
We’ve seen a basic use of the split() function. By default, split() uses whitespace to split strings. While whitespace works for some data, other characters often make more sense. It’s common to use a comma to separate values in a string.
Using commas, we can easily split a large amount of string data quickly. In the following example, a string containing all the instruments in a generic rock band is split into a new list.
Example 3: Using a Comma as the Separator
# use a comma as the separator
band = "Guitar,Piano,Drums,Trumpet,Bass Guitar,Vocals"
instruments = band.split(',')
print(instruments)
Output
['Guitar', 'Piano', 'Drums', 'Trumpet', 'Bass Guitar', 'Vocals']
Using a comma as the separator makes it easy to convert a string into a list. Converting strings into lists, and vice versa, can be a convenient method of storing and reading data.
Reading Text Files With Split() Function
I’ve created a text file on my computer called chess.txt. This file is located in the same folder as my Python file.
This text file contains a single line listing the pieces used in a game of chess. Notice that a comma is used to separate the names of the pieces.
King,Queen,Rook,Knight,Bishop,Pawn
By utilizing split(), we can easily separate the game pieces. We’ll use Python’s open() method to read the file and extract the line of text before splitting the string.
Example 4: Using split() to separate words in a text file
file = open("chess.txt")
pieces = file.read().split(',')
print(pieces)
Output
['King', 'Queen', 'Rook', 'Knight', 'Bishop', 'Pawn\n']
Using Strip() With the Split() Function
Looking back at the last example, you might notice something odd about our list. We’ve picked up some stray characters somewhere. Where did that \n come from?
This is the new line character. It tells Python that we’ve come to the end of the line in a text file.
We can make use of Python’s strip() method to remove the new line character from the end of our list. Using this method will ensure that we don’t pick up any pesky hidden characters from the file, such as the \n character.
file = open("chess.txt")
pieces = file.read().strip().split(',')
print(pieces)
file.close()
Output
['King', 'Queen', 'Rook', 'Knight', 'Bishop', 'Pawn']
Splitting Multiple Lines of Text
The above example was relatively basic. After all, we only had one line of text to deal with. But what if we have rows of data separated by commas? Let’s take a look at what reading a simple database might look like.
We’ll write a program for a teacher who needs to calculate the average grades for each student in his/her class.
Our client needs to keep track of his/her student’s homework. Specifically, he/she would like a way to read the data stored in a text file. The file, called grades.txt, contains the grades of the teacher’s students.
Our teachers_pet.py program will read the information from a file and build a string containing the grade data.
Once we have the string of grades, we can use the split() function to split the grades and store them in a list. After that, we’ll need to convert the strings to numbers using the float() method.
While this is admittedly a convoluted process, the example demonstrates the various ways one can work with numbers and strings in Python.
grades.txt
Noah, 81, 94, 100, 65
Elijah, 80, 84, 72, 79
Lucas, 95, 80, 89, 89
Emma, 95, 80, 80, 77
Ava, 90, 84, 85, 80
Amelia 100, 100, 95, 0
Example 5: The Teachers Pet Project
# teachers_pet.py
file = open("grades.txt", 'r')
grade_data = []
while(True):
line = file.readline()
if not line:
break
grade_data.append(line.split(','))
print(grade_data)
file.close()
# this function will loop through the student's grades
def calculate_averages(grades_data):
for grades in grade_data:
total = 0
# the student's name occupies the first spot
for i in range(1,len(grades)):
total += float(grades[i].strip()))
avg = total/float(len(grades)-1)
print("{} has an average of {}.".format(grades[0],avg))
calculate_averages(grade_data)
Output
Noah has an average of 85.0
Elijah has an average of 78.75
Lucas has an average of 88.25
Emma has an average of 83.0
Ava has an average of 84.75
Amelia has an average of 73.75
Our program has two parts. In the first part, we read the data from the file with the open() method. Line by line, the grades of each student are added to a list called grades_data.
We do this by taking advantage of float() to convert the string to a number. We’ll also use strip() to remove any unnecessary characters, such as the \n character.
Keep in mind that the first element in the list of grades is the student’s name.
We also need a function to calculate and print the grade averages. We do this in calculage_averages(). This handy function will look through the grade data and find each student’s grade point average.
Lessons for Further Study
Python comes with many built-in functions that can improve your code. The split() function is one such tool that can be used to break up difficult string problems.
Using a separator to divide the string allows you to convert string data into a list of strings, which is easier to manage. The split() function is also a quick way break up text data read from a file.
Learn more about Python and how to improve your programming skills by visiting the following tutorials.
- Learn how to use comments to improve your Python code
- How to use Python concatenation to join strings
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.