Get everything after last slash in a string in Python

avatar

Borislav Hadzhiev

Last updated: Jul 8, 2022

banner

Photo from Unsplash

Get everything after last slash in a string in Python #

To get everything after the last slash in a string:

  1. Use the str.rsplit() method to split the string on a slash, from the right.
  2. Get the list element at index 1.
  3. The method will return a new string that only contains the part after the last slash.
main.py
my_str = 'https://example.com/images/wallpaper.jpg' result = my_str.rsplit('/', 1)[1] print(result) # 👉️ 'wallpaper.jpg' with_slash = '/' + my_str.rsplit('/', 1)[1] print(with_slash) # 👉️ '/wallpaper.jpg' # 👇️ ['https://example.com/images', 'wallpaper.jpg'] print(my_str.rsplit('/', 1))

We used the str.rsplit() method to get everything after the last slash in 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 = 'https://example.com/images/wallpaper.jpg' # 👇️ ['https://example.com/images', 'wallpaper.jpg'] print(my_str.rsplit('/', 1)) # 👇️ ['https:', '', 'example.com', 'images', 'wallpaper.jpg'] print(my_str.rsplit('/'))

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 = 'https://example.com/images/wallpaper.jpg' result = my_str.rsplit('/', 1)[1] print(result) # 👉️ 'wallpaper.jpg' with_slash = '/' + my_str.rsplit('/', 1)[1] print(with_slash) # 👉️ '/wallpaper.jpg'
The last step is to access the list element at index 1 to get a string containing everything after the last occurrence of a slash.

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

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

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

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

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.