When working with Python strings, you can use several built-in string methods to obtain modified copies of strings, such as converting to uppercase, sorting a string, and more. One such method is .split() that splits a Python string into a list of strings, and we’ll learn more about it by coding examples. By the end of the tutorial, you will have learned the following:
how the .split() method workshow to customize the split using the sep and maxsplit parameters
Let’s begin!
Syntax of the split() Method in Python
Here’s the general syntax to use Python’s split() method on any valid string: Here, string can be any valid Python string. Both the sep and maxsplit parameters are optional.
sep denotes the separator on which you’d like to split the string. It should be specified as a string.maxsplit is an integer that specifies how many times you want to split the string.
Their default values are used when you don’t provide optional parameters.
When you don’t provide the sep value explicitly, whitespace is used as the default separator. When you don’t specify the value for maxsplit, it defaults to -1, which means that the string will be split on all occurrences of the separator.
Now that we’ve learned the syntax of the Python split() method let’s proceed to code some examples. The split() method splits a string maxsplit number of times on the occurrence of separator specified by the parameter sep.
Split a Python String into a List of Strings
If you have Python 3 installed on your machine, you can code with this tutorial by running the following code snippets in a Python REPL. To start the REPL, run one of the following commands from the terminal: ▶️ You can also try out these examples on Geekflare’s Python editor. In this example py_str is a Python string. Let’s call the .split() method on py_str without any parameters and observe the output. As seen above, the string is split on all occurrences of whitespace.
Split a Python String on the Occurrence of Separators
#1. As a first example, let’s split the string py_str with double underscores (__) as the separator. #2. Let’s take another example. Here, py_str has three sentences, each terminated by a period (.). ▶️ When we call the .split() method on this string, with ‘.’ as the separator, the resultant list has three sentences, as seen in the above code cell. #3. Let’s ask a few questions:
What happens when the separator never occurs in the string? How will the split occur in this case?
Here’s an example: We try to split py_str on the occurrence of asterisk (*)—which does not occur. As no split can be done in this case, the resultant list contains the entire string. In the next section, we’ll see how we can use the split() method on the contents of a text file.
Split the Contents of a Python File
When working with text files in Python, you may have to split the file’s contents—based on a separator—for easier processing. Here’s a sample text file: The code snippet below shows how to use split on the contents of the sample text file. The above code does the following:
Uses the with context manager to open and work with the text file ‘sample.txt’.Reads the contents of the file using the .read() method on the file object f.Splits the content on the occurrence of the separator ellipsis (…) into a list str_list.Loops through str_list to access each string and prints it out.
Here’s the output. As an exercise, you can try splitting the contents of a text file on any separator of choice.
Split a Python String into Chunks
When you split a string once, you’ll get two chunks; splitting it twice will get three. This is illustrated below. #1. We set maxsplit equal to 1. We haven’t specified a separator, so the split will occur on whitespaces by default. Even though the second chunk in the list contains whitespaces, the split does not occur because the split is now controlled by the maxsplit value of one. #2. Let’s increase the maxsplit value to 2 and observe how the split occurs for the following example. As with the previous example, the maxsplit value decides the number of splits made. We get three chunks, splits after whitespace’s first and second occurrences. #3. What happens if you set maxsplit to a value greater than the number of occurrences of the separator? In the following code cell, we set maxsplit it to 8 when the string contains only four commas. Here, the split method splits py_str on all four occurrences of a comma. Even if you try setting maxsplit to a value less than -1, say, -7, the split will be done on all occurrences of the separator. Next, let’s put together all that we have learned and use both the sep maxsplit parameters.
Split a Python String into Chunks on a Separator
#1. Suppose we need to split the string py_str into three chunks on the occurrence of comma (,). To do this, we can set the sep value to ‘,’ and maxsplit value to 2 in the method call. As seen in the output, the split occurs twice on the first two occurrences of the separator. #2. The separator sep does not always have to be a special character. It can be a sequence of special characters, like the double underscores we used earlier, or it could even be a substring. Let us set the string ‘learn’ as the sep argument and see how the split occurs for varying values of maxsplit. Here, we set maxsplit to 2. #3. If you’d like to split py_str on all occurrences of the string ‘learn’, we can call this .split() method by setting sep = ’learn’—without the maxsplit parameter. This is equivalent to explicitly set the maxsplit value to -1, as shown in the code cell below. We see that the split occurs in all occurrences of ‘learn’.
Wrapping Up
I hope you’ve now understood how to use the .split() method with Python strings. Here’s a summary of this tutorial:
Python’s built-in .split() method splits a string into a list of strings.Use string.split() to split the string on all occurrences of the default separator, whitespace.Use string.split(sep,maxsplit) to split the string maxsplit number of times on the occurrence of separator sep. The resultant list has maxsplit+1 items.
As a next step, you can learn how to check if Python strings are palindromes or anagrams.