Hướng dẫn python split last value

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.

Split a string and get the last element in Python #

Use the str.rsplit() method with maxsplit set to 1 to split a string and get the last element. The rsplit() method splits from the right and will only perform a single split when maxsplit is set to 1.

Copied!

my_str = 'one,two,three,four' last = my_str.rsplit(',', 1)[-1] print(last) # 👉️ 'four'

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!

my_str = 'one two three' print(my_str.rsplit(' ')) # 👉️ ['one', 'two', 'three'] print(my_str.rsplit(' ', 1)) # 👉️ ['one two', 'three']

The method takes the following 2 arguments:

NameDescription
separator Split the string into substrings on each occurrence of the separator
maxsplit At most maxsplit splits are done, the rightmost ones (optional)

Except for splitting from the right, rsplit() behaves like split().

When the maxsplit argument is set to 1, at most 1 split is done.

The last step is to access the last element in the list by accessing the list item at index -1.

Copied!

my_str = 'one,two,three,four' last = my_str.rsplit(',', 1)[-1] print(last) # 👉️ 'four'

You can also use the str.split() method in a similar way.

Copied!

my_str = 'one-two-three-four' last = my_str.split('-')[-1] print(last) # 👉️ 'four'

If your string ends with the specific separator, you might get a confusing result.

Copied!

my_str = 'one-two-three-four-' last = my_str.rsplit('-', 1)[-1] # 👇️ ['one-two-three-four', ''] print(my_str.rsplit('-', 1)) print(last) # 👉️ ""

You can use the str.strip() method to remove the leading or trailing separator.

Copied!

my_str = 'one-two-three-four-' last = my_str.strip('-').rsplit('-', 1)[-1] print(last) # 👉️ "four"

We used the str.strip() method to remove any leading or trailing hyphens from the string before calling the rsplit() method.