Intro

str.split([sep[, maxsplit]])

There is splitting of string method that is used to divide string into substring list. This split method has 2 optional parameters. If there is not given (or non-exist), string will be divided into substring using whitespace as the delimiter which is each of the substring that is pure consist of space that is used as delimiter.

Untitled

Implementation

Now let’s see string that came from file calc of Excel or OpenOffice. We have seen the example that split take white space as default delimiter. We want to divide string into smaller example using this coma as delimiter. The only thing we need to do is using “,” as the argument of split ():

line = "Wildan;Azzam;Shalahuddin"
line.split(";")
['Wildan', 'Azzam', 'Shalahuddin']

Split method() has other optional parameter: max.split. If maxsplit is given, the maxsplit at the most is done. It means the list that is generated will have more element “maxsplit+1”. We will illustrate operation mode of maxsplit in the example.

mammon = "The god of the world's leading religion. The chief temple is in the holy city of New York."
mammon.split(" ",3)
['The',
 'god',
 'of',
 "the world's leading religion. The chief temple is in the holy city of New York."]

So, we use void as a delimiter string on the previous example that can be an issue. If some voids or space is connected, split() will divide string after each of void so it will get string and void string only with tab in the (”\t”) in the result of our list:

Example: