Divide the line by 2 based on the last occurrence of the delimiter

I would like to know if there is any built-in function in python to split the string into 2 parts based on the last occurrence of the separator.

for example: consider the line "abc, d, e, f" after the separator ",", I want the result to be like

"abc, d, e" and "f".

I know how to manipulate a string to get the desired result, but I want to know if there is any built-in function in python.

+63
python string
Sep 08 '11 at 16:56
source share
3 answers

Use rpartition(s) . He does just that.

You can also use rsplit(s, 1) .

+75
Sep 08 '11 at 16:59
source share
 >>> "abc,d,e,f".rsplit(',',1) ['abc,d,e', 'f'] 
+50
Sep 08 '11 at 16:59
source share

rsplit

 >>> "abc,d,e,f".rsplit(',', 1) ['abc,d,e', 'f'] 
+33
Sep 08 '11 at 16:59
source share



All Articles