Open In App

Split on last occurrence of delimiter-Python

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:


Next Article
Practice Tags :

Similar Reads