Web Development Course:
TL;DR – Python split() divides a string into a list of strings using a specified delimiter.
How Python split() works
String variables are useful tools for any Python programmer. They can contain numeric or alphanumeric information and are commonly used to store data directories or print messages.
.split() Python function is a commonly-used string manipulation tool.
If you’ve already tried joining two strings in Python by concatenation, then
split() does the exact opposite of that.
It scans through a string and separates it whenever the script comes across a pre-specified separator. It operates as follows using two parameters:
separator parameter tells Python where to break the string. You don’t, in fact, always need to use it. If no delimiter is set, Python will use any whitespace in the string as a separation point instead.
maxsplit defines how many times the string can be broken up. If you don’t add any value here, Python will scan the entire string length and separate it whenever a delimiter is found.
Examples of using split() in Python
One of the easiest examples of how to divide Python strings with
split() is to assign no parameters and break down a string of text into individual words:
text = 'The quick brown fox jumps over the lazy dog' # Split the text wherever there's a space. words = text.split() print(words)
The code above produces the following output:
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
split() function can extract multiple pieces of information from an individual string and assign each to a separate variable. The following example presents a paragraph and turns each sentence into a variable:
paragraph = 'The quick brown fox jumps over the lazy dog. The quick brown dog jumps over the lazy fox' # Split the text wherever there's a full stop. a,b = paragraph.split('.') # Display the results. print(a) print(b)
How maxsplit affects string splitting in Python
Don’t forget to use the
maxsplit parameter if you only want to break up the first part of a string with the
split() Python operator:
cars= 'Audi and Kia and BMV and Volvo and Opel' # maxsplit of 1 print(cars.split(' and ',1)) # maxsplit of 2 print(cars.split(' and ',2)) # maxsplit of 1 print(cars.split(' and ',3)) # maxsplit of 1 print(cars.split(' and ',4))
The output of the code snippet above will be:
['Audi', 'Kia and BMV and Volvo and Opel'] ['Audi', 'Kia', 'BMV and Volvo and Opel'] ['Audi', 'Kia', 'BMV', 'Volvo and Opel'] ['Audi', 'Kia', 'BMV', 'Volvo', 'Opel']
Python split(): useful tips
- If you do specify
maxsplitand there are an adequate number of delimiting pieces of text in the string, the output will have a length of
- Recombining a string that has already been split in Python can be done via string concatenation.
split()only works on string variables. If you encounter problems with
split(), it may be because you are trying to call it upon a non-string object. If this happens, you can force Python to treat the variable as a string with
Splitting Datasets With the Sklearn train_test_split Function
This tutorial on train_test_split covers the way to divide datasets into two parts: for testing and training with the Sklearn train_test_split function.