Splitting on first occurrence

Acorn picture Acorn · Aug 1, 2011 · Viewed 281.4k times · Source

What would be the best way to split a string on the first occurrence of a delimiter?

For example:

"123mango abcd mango kiwi peach"

splitting on the first mango to get:

"abcd mango kiwi peach"

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Aug 1, 2011

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]