Remove everything before the last Forward Slash in Python

avatar

Borislav Hadzhiev

Last updated: Jul 8, 2022

banner

Photo from Unsplash

Remove everything before the last Forward Slash in Python #

To remove everything before the last forward slash:

  1. Use the str.rsplit() method to split the string on a forward slash, from the right.
  2. Access the list item at index 1.
  3. The result will be a string containing everything after the last forward slash.
main.py
my_str = 'example.com/images/dog.png' result = my_str.rsplit('/', 1)[1] print(result) # 👉️ 'dog.png' # 👇️ if you want to include the forward slash in the result result_2 = '/' + my_str.rsplit('/', 1)[1] print(result_2) # 👉️ '/dog.png' # 👇️ ['example.com/images', 'dog.png'] print(my_str.rsplit('/', 1))
We used the str.rsplit() method to remove everything before the last forward slash from a string.

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 = 'example.com/images/dog.png' # 👇️ ['example.com', 'images', 'dog.png'] print(my_str.rsplit('/')) # 👇️ ['example.com/images', 'dog.png'] print(my_str.rsplit('/', 1))

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/images/dog.png' result = my_str.rsplit('/', 1)[1] print(result) # 👉️ 'dog.png' # 👇️ if you want to include the forward slash in the result result_2 = '/' + my_str.rsplit('/', 1)[1] print(result_2) # 👉️ '/dog.png'

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.

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

main.py
my_str = 'example.com/images/dog.png' result = my_str.rpartition('/')[2] print(result) # 👉️ dog.png # 👇️ ('example.com/images', '/', 'dog.png') 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 forward slash in the result, use the str.join() method to join the second and third list items.

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

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.