Split on last occurrence of delimiter-Python
Last Updated :
30 Apr, 2025
The goal here is to split a string into two parts based on the last occurrence of a specific delimiter, such as a comma or space. For example, given the string "gfg, is, good, better, and best", we want to split it into two parts, everything before the last comma-space and everything after it. The result will be ['gfg, is, good, better', 'and best']. Let's explore different methods to achieve this in Python.
Using rsplit()
rsplit() method splits a string starting from the right end. It accepts an optional maxsplit argument to limit the number of splits. By using rsplit(', ', 1), the string is split only at the last occurrence of the comma followed by a space, making it ideal for breaking the string into two parts from the end.
Python
s = "gfg, is, good, better, and best"
res = s.rsplit(', ', 1)
print(res)
Output['gfg, is, good, better', 'and best']
Explanation: s.rsplit(', ', 1) splits the string from the right, using ', ' as the delimiter and performing only one split at the last occurrence.
Using rfind()
rfind() method returns the index of the last occurrence of a specified substring. With this index, we can use string slicing to separate the string into two parts, everything before and after the last comma-space.
Python
s = "gfg, is, good, better, and best"
idx = s.rfind(', ')
res = [s[:idx], s[idx + 2:]]
print(res)
Output['gfg, is, good, better', 'and best']
Explanation:
- rfind(', ') finds the last occurrence of the delimiter , and returns its index.
- [s[:idx], s[idx + 2:]] splits the string into two parts, everything before the last comma-space and everything after it (excluding the delimiter).
Using re.split()
re.split() method from the re module allows for splitting a string based on a regular expression pattern. When combined with maxsplit=1, it splits the string at the first match from the left. While it's powerful for pattern-based splitting, it's not naturally suited for splitting from the right.
Python
import re
s = "gfg, is, good, better, and best"
res = re.split(r',\s(?=[^,]*$)', s)
print(res)
Output['gfg', 'is, good, better, and best']
Explanation: re.split(r',\s(?=[^,]*$)', s) splits the string at the last ', ', with ,\s matching the comma and space and (?=[^,]*$) ensuring no further commas appear after it, dividing the string into two parts.
Using join() and split()
This method involves first splitting the string using split(', '), which breaks it into a list of parts. Then, we use join() to recombine all parts except the last, effectively isolating the last item. It's a neat way to mimic right-sided splitting when rsplit() isn't used.
Python
s = "gfg, is, good, better, and best"
p = s.split(', ') # parts
res = [', '.join(p[:-1]), p[-1]]
print(res)
Output['gfg, is, good, better', 'and best']
Explanation: [', '.join(p[:-1]), p[-1]] combines all parts except the last one using join() and separates the last part, resulting in two parts, everything before the last item and the last item.
Related Articles:
Similar Reads
Python - Avoid Last occurrence of delimiter Given an Integer list, perform join with the delimiter, avoiding the extra delimiter at the end. Input : test_list = [4, 7, 8, 3, 2, 1, 9], delim = "*" Output : 4*7*8*3*2*1*9 Explanation : The rear "*" which usually occurs in concatenation, is avoided. Input : test_list = [4, 7, 8, 3], delim = "*" O
7 min read
Python - Slice from Last Occurrence of K Sometimes, while working with Python Strings, we can have a problem in which we need to perform the task of performing characters stripping on the last occurrence of element. This can have applications in which data is involved. Lets discuss certain ways in which this task can be performed. Method #
3 min read
Split string on Kth Occurrence of Character - Python The task is to write Python program to split a given string into two parts at the KáµÊ° occurrence of a specified character. If the character occurs fewer than K times return the entire string as the first part and an empty string as the second part. For example, in the string "a,b,c,d,e,f", splitting
3 min read
Python | Find last occurrence of substring Sometimes, while working with strings, we need to find if a substring exists in the string. This problem is quite common and its solution has been discussed many times before. The variation of getting the last occurrence of the string is discussed here. Let's discuss certain ways in which we can fin
8 min read
Python - Split List on next larger value Given a List, perform split on next larger value. Input : test_list = [4, 2, 3, 7, 5, 1, 3, 4, 11, 2] Output : [[4, 2, 3], [7, 5, 1, 3, 4], [11, 2]] Explanation : After 4, 7 is greater, split happens at that element, and so on. Input : test_list = [4, 2, 3, 7, 5, 1, 3, 4, 1, 2] Output : [[4, 2, 3],
2 min read