How to use the Python split() method of string class?
In Python, the split() method is a member of the string class. To tokenize a string, a programmer invokes a string object’s split() method with the delimiter passed as an argument. The delimiter can be a single character (e.g., ‘,’) or a string (e.g., ‘XYZ’) itself.
The string is converted into a list in response to a successful call to the split method. Each list element represents a substring from the original string, separated by the delimiter.
list object = str.split(separator, maxsplit)
str– a string that needs to be split.
separator(Optional) – Delimiter string, by default, is
whitespace. The separator can also take the value of 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, and breaks the string wherever a delimiter is present. There will be a total
maxsplit+ 1 element in the output list.
Return Value -An object of Python list class that contains substrings.
Following is the basic example of a split, where no delimiter is passed. In this case, the string splits using a default delimiter(whitespace).
str = "I want to learn python programming language."; print(str.split());
['I', 'want', 'to', 'learn', 'python', 'programming', 'language.']
How does the Python split () method work?
In the split function, the string is read from left to right. When it encounters a delimiter, the left substring is added to the output list, and the right substring is then passed again to the split method. It is an example of a recursive approach, where a function calls itself.
The read continues until the controls reach the end of the string or the maximum number of splits has been reached. If there is no occurrence of a separator, the whole string is returned as a list having a single element.
What is a real-world example of splitting a string?
Numerous examples exist where a program needs to break a string into substrings 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, etc., after reading the line and splitting it by a comma, the program can calculate each employee’s Salary for the month.
Although, it is also feasible in other ways too. But it makes sense to use the inbuild method split.
Below are some more examples of use cases 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 along with a delimiter.
str = "We#want#to#learn#Pyhon"; print(str.split('#',2));
['We', 'want', 'to#learn#Pyhon']
As shown in the example above, the
maxsplit limit is two. Because of that, there will be only two splits in the string. As can be seen in the output, on the first split, ‘We’ is added to the list, and on the second, it adds ‘want’ and then stops. In the list, the remaining string, ‘to#learn#Pyhon,’ is added as a third substring.