Use .rsplit() or .rpartition() instead:
s.rsplit(',', 1) s.rpartition(',')str.rsplit() lets you specify how many times to split, while str.rpartition() only splits once but always returns a fixed number of elements (prefix, delimiter & postfix) and is faster for the single split case.
Demo:
>>> s = "a,b,c,d" >>> s.rsplit(',', 1) ['a,b,c', 'd'] >>> s.rsplit(',', 2) ['a,b', 'c', 'd'] >>> s.rpartition(',') ('a,b,c', ',', 'd')Both methods start splitting from the right-hand-side of the string; by giving str.rsplit() a maximum as the second argument, you get to split just the right-hand-most occurrences.
If you only need the last element, but there is a chance that the delimiter is not present in the input string or is the very last character in the input, use the following expressions:
# last element, or the original if no `,` is present or is the last character s.rsplit(',', 1)[-1] or s s.rpartition(',')[-1] or sIf you need the delimiter gone even when it is the last character, I'd use:
def last(string, delimiter): """Return the last element from string, after the delimiter If string ends in the delimiter or the delimiter is absent, returns the original string without the delimiter. """ prefix, delim, last = string.rpartition(delimiter) return last if (delim and last) else prefixThis uses the fact that string.rpartition() returns the delimiter as the second argument only if it was present, and an empty string otherwise.
The splitting of strings has always been discussed in various applications and use cases. One of the interesting variations 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 rsplit(str, 1) The normal string split can perform the split from the front, but Python also offers another method that can perform this very task from the rear end, hence increasing the versatility of applications.
Python3
test_string = "gfg, is, good, better, and best"
print("The original string : " + str(test_string))
res = test_string.rsplit(', ', 1)
print("The splitted list at the last comma : " + str(res))
Output :
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 rpartition() This function can also perform the desired reverse partition, but the drawbacks to using this is the construction of additional delimiter value and also the speed is slower than the above method and hence not recommended.
Python3
test_string = "gfg, is, good, better, and best"
print("The original string : " + str(test_string))
res = test_string.rpartition(', ')
print("The splitted list at the last comma : " + str(res))
Output :
The original string : gfg, is, good, better, and best The splitted list at the last comma : ('gfg, is, good, better', ', ', 'and best')Method #3 : Using split() and replace() methods.
Python3
test_string = "gfg, is, good, better, and best"
print("The original string : " + str(test_string))
p=test_string.count(",")
c=0
new=""
for i in test_string:
if(i=="," and c<p-1):
new+="*"
c+=1
else:
new+=i
x=new.split(",")
x[0]=x[0].replace("*",",")
print("The splitted list at the last comma : " + str(x))
Output
The original string : gfg, is, good, better, and best The splitted list at the last comma : ['gfg, is, good, better', ' and best']