Splitting on first occurrence Splitting on first occurrence python python

Splitting on first occurrence

From the docs:

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

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements).

s.split('mango', 1)[1]

>>> s = "123mango abcd mango kiwi peach">>> s.split("mango", 1)['123', ' abcd mango kiwi peach']>>> s.split("mango", 1)[1]' abcd mango kiwi peach'

For me the better approach is that:

s.split('mango', 1)[-1]

...because if happens that occurrence is not in the string you'll get "IndexError: list index out of range".

Therefore -1 will not get any harm cause number of occurrences is already set to one.