Splitting on last delimiter in Python string?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python's str.rsplit() method splits a string from the right, making it the direct solution for splitting on the last occurrence of a delimiter. With maxsplit=1, it splits only on the last delimiter, producing two parts. The companion method str.rpartition() always returns three parts (before, delimiter, after) and never raises an error. Both are more readable and reliable than manual approaches using rfind().
rsplit() with maxsplit=1
rsplit() works identically to split() except it starts splitting from the right side of the string.
rpartition()
rpartition() always returns a 3-tuple: (before, separator, after):
If the delimiter is not found, rpartition() returns ('', '', original_string):
This makes it safe to use without checking whether the delimiter exists.
Practical Examples
Splitting File Extension
Splitting Domain from URL Path
Splitting Package from Class Name
Splitting on Multi-Character Delimiter
rsplit() vs rpartition()
| Feature | rsplit(sep, 1) | rpartition(sep) |
| Return type | List of 2 elements | Tuple of 3 elements |
| Delimiter in result | No | Yes (middle element) |
| Delimiter not found | Returns [original] | Returns ('', '', original) |
| Multiple splits | Yes (adjust maxsplit) | No (always one split) |
Using rfind() Manually
This works but is verbose compared to rsplit() or rpartition(). Use it only when you need the index position itself.
split() vs rsplit() Comparison
The difference only matters when maxsplit limits the number of splits.
Edge Cases
Common Pitfalls
- Forgetting
maxsplit=1:rsplit(".")withoutmaxsplitsplits on ALL dots, same assplit("."). Addmaxsplit=1to split only on the last occurrence. - Unpacking
rsplitwhen delimiter is missing:a, b = "nodot".rsplit(".", 1)raisesValueErrorbecause the result is['nodot'](one element, not two). Check the length first or userpartition()which always returns three values. - Using
splitinstead ofrsplit:"a.b.c".split(".", 1)gives['a', 'b.c'](splits on first dot).rsplit(".", 1)gives['a.b', 'c'](splits on last dot). Choose based on which part you want to keep intact. os.path.splitextfor file extensions: For file paths,os.path.splitext()is more correct thanrsplit(".")because it handles edge cases like dotfiles (.bashrc) and no-extension files.rpartitionincludes the delimiter: Unlikersplit,rpartitionreturns the delimiter as the middle element. If you do not need it, use_to discard:before, _, after = text.rpartition(".").
Summary
- Use
str.rsplit(delimiter, maxsplit=1)to split on the last occurrence of a delimiter - Use
str.rpartition(delimiter)for a safe split that always returns 3 parts rsplitwithoutmaxsplitis the same assplit— always specifymaxsplit=1for "last delimiter" behaviorrpartitionis safer for unpacking because it never changes the number of return values- Use
os.path.splitext()for file extension splitting instead of manual string splitting
Related reading
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.