Python String split() Method



The Python String split() method splits all the words in a string separated by a specified separator. This separator is a delimiter string, and can be a comma, full-stop, space character or any other character used to separate strings.

Usually, if multiple separators are grouped together, the method treats it as an empty string. But if the separator is not specified or is None, and the string consists of consecutive whitespaces; they are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator results in an empty string.

Syntax

Following is the syntax for Python String split() method −

str.split(str="", num=string.count(str)).

Parameters

  • sep − This is any delimeter, by default it is space.

  • maxsplit − this is number of lines minus one

Return Value

This method returns a list of lines.

Example

When we pass no arguments to the method, default character (space) is considered as an argument. In this case, the split() method returns the list of lines after separating the string.

str = "Line1-abcdef \nLine2-abc \nLine4-abcd";
print(str.split( ))
print(str.split(' ', -1))

When we run above program, it produces following result. For the first case, even the other delimiters, like line separators (\n), are removed.

['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
['Line1-abcdef', '\nLine2-abc', '\nLine4-abcd']

Example

Well-known delimiters are passed as arguments to the method, to obtain the result value as the list of lines separated.

In this example, we take two strings as input; both containing delimiters. We call the split() method on both strings by passing a required delimiter as an argument.

str1 = "abcde, 12345, !@#$%";
str2 = "14<65<189<235<456"
print(str1.split(','))
print()
print(str2.split('<'))

The output for the above program is given below −

['abcde', '12345', '!@#$%']
['14', '65', '189', '235', '456']

Example

In the following example we are separating the integer and decimal parts of a decimal number using the split() method by passing the dot(.) as an argument.

str = "123.748289";
print("Separating the integer and decimal from the input number:")
print(str.split('.'))

Let us execute the program above to obtain the output as follows −

Separating the integer and decimal from the input number:
['123', '748289']

Example

When we pass the separator parameter, but the separator parameter is grouped together in the string; the method treats it as an empty string.

In the example below, we are creating a string with the value: "aaa,,ccc,ddd,eee" and, called the split() method on it with comma (",") as as argument. Since the given string contains commas grouped together; the method returns empty strings for each comma separator present.

str = "aaa,,ccc,ddd,eee";
print(str.split(','))

The output for the program above is displayed below −

['aaa', '', 'ccc', 'ddd', 'eee']

Example

When we pass maxsplit parameter, the method returns a list of lines separated up to the index specified.

str = "aaa,bbb,ccc,ddd,eee";
print(str.split(',', 2))

If we execute the program above, the output is achieved as −

['aaa', 'bbb', 'ccc,ddd,eee']
python_strings.htm
Advertisements