Python: Split a string on the last occurrence of the delimiter| split string last occurrence python

Python Split a string on the last occurrence of the delimiter

Write a python program to split a string on the last occurrence of the delimiter.

Sirf Padhai

Program:-

str1 = "s,i,r,f,p,a,d,h,a,i"
print(str1.rsplit(',' , 1)
print(str1.rsplit(',' , 2)
print(str1.rsplit(',' , 5)

Output:-

's,i,r,f,p,a,d,h,a','i'
's,i,r,f,p,a,d,h', 'a','i'
's,i,r,f,p', 'a', 'd', 'h', 'a','i'

Previous articleReverse string in python | Syntax for reverse() List Method in Python.
Next articlePython: move spaces to the front of a given string

LEAVE A REPLY

Please enter your comment!
Please enter your name here