Python String split() Method

Splits the string by the given separator, from left to right. An array is returned for the separated strings. An optional number of occurrences can be specified.

Syntax

Python
string.split(separator = " ", max_strings = -1)

Parameters

ParameterDescription
separator Optional. The string by which to split the given string. The default is a space (" ")
max_strings Optional. The maximum number of strings to return. The default is -1 which means "all strings that resulted from the split operation".

Example

Python
str = 'You say tomato, I say tomato, the French say tomate.'
print(str.split('tomato'))
print(str.split('tomato', 1))

Output

['You say ', ', I say ', ', the French say tomate.']
['You say ', ', I say tomato, the French say tomate.']

Important Note

If you specify max_strings then the number of items that will be returned is max_strings+1.