The splitting of string has always been discussed in various applications and use cases. One of the interesting variation of list splitting can be splitting the list on delimiter but this time only on the last occurrence of it. Let’s discuss certain ways in which this can be done.
Method #1 : Using
The normal string split can perform the split from the front, but Python also offers another method which can perform this very task from the rear end, and hence increasing the versatility of applications.
The original string : gfg, is, good, better, and best The splitted list at the last comma : ['gfg, is, good, better', 'and best']
Method #2 : Using
This function can also perform the desired reverse partition, but the drawbacks to use this is construction of additional delimiter value and also the speed is slower than above method and hence not recommended.
The original string : gfg, is, good, better, and best The splitted list at the last comma : ('gfg, is, good, better', ', ', 'and best')
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Ways to split strings using newline delimiter
- Python | Pandas str.join() to join string/list elements with passed delimiter
- Python - Replace delimiter
- Python - Convert delimiter seperated Mixed String to valid List
- Python - Convert List to delimiter separated String
- Python - Convert Delimiter separated list to Number
- Python - Segregate elements by delimiter
- Python - Construct dictionary Key-Value pairs separated by delimiter
- Python - Concatenate Tuple elements by delimiter
- Python program to convert a list of strings with a delimiter to a list of tuple
- Python - Sort words separated by Delimiter
- How to read a CSV file to a Dataframe with custom delimiter in Pandas?
- Python | Split string on Kth Occurrence of Character
- Python - Split Strings on Prefix Occurrence
- Python | Last occurrence of some element in a list
- Python | Find last occurrence of substring
- Python - Slice from Last Occurrence of K
- Python - Avoid Last occurrence of delimitter
- Python | Pandas Split strings into two List/Columns using str.split()
- Generate two output strings depending upon occurrence of character in input string in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.