The split method
The split method returns a list of a string split on the left by the first match of a substring and the separator specified in the parameter. In the second optional parameter, we specify how many times to split the string. By default, you can split the string an unlimited number of times.
Syntax
string.split(separator, [number of divisions])
Example
Let's apply the split method to the following string and split it once:
txt = 'ab_ac_dea'
print(txt.split('_', 1))
Result of code execution:
['ab', 'ac_dea']
Example
Now let's apply the split method without specifying the number of divisions:
txt = 'ab_ac_dea'
print(txt.split('_'))
Result of code execution:
['ab', 'ac', 'dea']
See also
-
method
rsplit,
which divides a string by the substring on the right -
method
rpartition,
which divides a string by the last match of a substring -
method
partition,
which divides a string by the first match of a substring -
method
join,
which returns a string from a list of strings