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


This program is using the rsplit() method in Python to split a string "T,u,t,o,r,J,o,e,s". The rsplit() method is used to split the string from the right, i.e., starting from the end of the string. The first argument to the rsplit() method is the separator, in this case a comma (','). The second argument is the number of times the separator should be used to split the string.

In the first print statement, the rsplit() method is called with the argument 4 for the second parameter. This means that the string "T,u,t,o,r,J,o,e,s" will be split into 4 parts starting from the end of the string, using the comma as the separator.

In the second print statement, the rsplit() method is called with the argument 2 for the second parameter. This means that the string "T,u,t,o,r,J,o,e,s" will be split into 2 parts starting from the end of the string, using the comma as the separator.

Source Code

str1 = "T,u,t,o,r,J,o,e,s"
print(str1.rsplit(',', 4))
print(str1.rsplit(',', 2))

Output

['T,u,t,o,r', 'J', 'o', 'e', 's']
['T,u,t,o,r,J,o', 'e', 's']


Example Programs