How do you split a string at the last delimiter in python?

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 s

If 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 prefix

This 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']


How do you split a string at the end in Python?

We used the rsplit() method to split the string from the right. The str. rsplit method returns a list of the words in the string using the provided separator as the delimiter string. Copied! ... .

How do you split the last character of a string?

To split a string on the last occurrence of a substring:, use the lastIndexOf() method to get the last index of the substring and call the slice() method on the string to get the portions before and after the substring you want to split on.

How do you split a string to the left and right in Python?

The rsplit() method splits a string into a list, starting from the right. If no "max" is specified, this method will return the same as the split() method. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Chủ đề