Python split function

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’).

Python Split Function
Python Split Function

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 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());

Output->

['I', 'want', 'to', 'learn', 'python', 'programming', 'language.']

How does Split() function work?

A call to 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 string or max splits completes. If it will not find any separator, returns a whole string as a list.

A read world example for 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('#'));

Output->

['We', 'want', 'to', 'learn', 'Pyhon']

Split() function example when max splits are given –

str = "We#want#to#learn#Pyhon";
 print(str.split('#',2));

Output->

['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, on the first split ‘We’ added to the list, on the second it adds ‘want’ and stops. The remaining string, ‘to#learn#Pyhon’ added as it is.