Remove everything before last occurrence of character (Python)

avatar

Borislav Hadzhiev

Last updated: Jul 7, 2022

banner

Check out my new book

Remove everything before last occurrence of character (Python) #

To remove everything before the last occurrence of a character:

  1. Use the str.rsplit() method to split the string from the right.
  2. Access the list item at index 1.
  3. The result will be a string containing everything after the last occurrence of the character.
main.py
my_str = 'example.com/articles/python' result = my_str.rsplit('/', 1)[1] print(result) # 👉️ 'python' # 👇️ if you want to include the character in the result result_2 = '/' + my_str.rsplit('/', 1)[1] print(result_2) # 👉️ '/python' # 👇️ ['example.com/articles', 'python'] print(my_str.rsplit('/', 1))

We used the str.rsplit() method to remove everything before the last occurrence of a character.

The str.rsplit method returns a list of the words in the string using the provided separator as the delimiter string.

main.py
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
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done, the rightmost ones (optional)

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

Notice that we provided a value of 1 for the maxsplit argument because we only want to split the string once, from the right.
main.py
my_str = 'example.com/articles/python' result = my_str.rsplit('/', 1)[1] print(result) # 👉️ 'python' # 👇️ ['example.com/articles', 'python'] print(my_str.rsplit('/', 1))

The last step is to access the list element at index 1 to get a string containing everything after the last occurrence of the specified character.

If you want to include the character in the result, use the addition (+) operator.

main.py
my_str = 'example.com/articles/python' result_2 = '/' + my_str.rsplit('/', 1)[1] print(result_2) # 👉️ '/python'

Alternatively, you can use the str.rpartition() method.

main.py
my_str = 'example.com/articles/python' result = my_str.rpartition('/')[2] print(result) # 👉️ 'python' # 👇️ ('example.com/articles', '/', 'python') print(my_str.rpartition('/'))

The str.rpartition method splits the string at the last occurrence of the provided separator.

The method returns a tuple containing 3 elements - the part before the separator, the separator, and the part after the separator.

If the separator is not found in the string, the method returns a tuple containing two empty strings, followed by the string itself.

If you need to include the separator in the result, use the str.join() method to join the second and third list items.

main.py
my_str = 'example.com/articles/python' result = ''.join(my_str.rpartition('/')[1:]) print(result) # 👉️ '/python'

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.