How to use the Python split() function for string?
Split() is a built-in function in the string class of Python. When a programmer needs to tokenize a string, it calls the Split() method from the string object with a delimiter. A delimiter can be a single character (e.g ‘,’) or it could be another string (e.g ‘XYZ’).
After the successful call, the python function split the string into a list, where each element of the list is a token from the main string, separated by the delimiter. Following is the syntax for the library function.
list object = str.split(separator, maxsplit)
- str– a string that needs to be split.
- separator (Optional) – Delimiter string, by default it can be any whitespace characters e.g tab (t), newline(n), etc.
- maxsplit (Optional)– The maximum number of splits. Each delimiter occurrence does a single split. By default, its value is -1, which means no limits on the splits, break the string wherever a delimiter is present. There will be a total
maxsplit+ 1 element in the output list.
Return Value – A list object of strings.
Following is the basic example of a split, where the string splits against the default delimiters.
str = "I want to learn python programming language."; print(str.split());
['I', 'want', 'to', 'learn', 'python', 'programming', 'language.']
How does Split() function work?
A call to the split function starts reading the string from left to right. Once encounters a delimiter (or default ), its left substring is added to the output list and the right substring is again passed to function. It is an example of a recursive approach, where a function is calling itself.
It continues, till the controls reach the end of the string or max splits are completed. If it will not find any separator, returns a whole string as a list.
What is a real-world example of splitting a string?
There could be numerous examples where we need to break a string into parts based on a separator pattern. For example, if a program reads an employee CSV file.
Where each line contains the details for an employee in a comma-separated value e.g. Name, Employee Id, Hours Worked, Leaves, Bonus. After reading the line and splitting it by a comma, the program can calculate the Salary for the month for each employee.
Although, it is also feasible in other ways too. But it makes sense to use of library function split.
More examples for split():
Example with a delimiter.
str = "We#want#to#learn#Pyhon"; print(str.split('#'));
['We', 'want', 'to', 'learn', 'Pyhon']
Split() function example when max splits are given –
str = "We#want#to#learn#Pyhon"; print(str.split('#',2));
['We', 'want', 'to#learn#Pyhon']
In the above example, the max limit is 2. This means the string will be split only twice. We can see in the output, that on the first split ‘We’ is added to the list, on the second it adds ‘want’ and stops. The remaining string, ‘to#learn#Pyhon’ is added as it is.