Skip to main content

split()

The split() method in Python is a String method used to split a string into a list of substrings based on a specified delimiter. It takes the delimiter as an argument and returns a list of substrings separated by that delimiter.

Parameter Values

Parameter Description
sep

The sep parameter is the delimiter by which the given string will be split. If not provided, whitespace is considered as the default delimiter.

maxsplit

The maxsplit parameter defines the maximum number of splits to be done, with the remainder of the string being returned as the final element of the list. If not provided, all occurrences of the delimiter are used to split the string.

Return Values

The split() method returns a list of strings resulting from the split operation.

How to Use split() in Python

Example 1:

The split() method splits a string into a list using a specified separator. By default, the separator is a space.

text = 'hello world, my name is Alice'
split_text = text.split(', ')
print(split_text)
Example 2:

You can also specify the maximum number of splits that the split() method can make.

text = 'apple,banana,cherry,dates'
split_text = text.split(',', 2)
print(split_text)