Python substring after last occurrence of character

Use os.path.basename() instead and not worry about the details.

os.path.basename() returns the filename portion of your path:

>>> import os.path
>>> os.path.basename('folder1/folder2/folder3/new_folder/image.jpg')
'image.jpg'

For a more generic string splitting problem, you can use str.rpartition() to split a string on a given character sequence counting from the end:

>>> 'foo:bar:baz'.rpartition(':')
('foo:bar', ':', 'baz')
>>> 'foo:bar:baz'.rpartition(':')[-1]
'baz'

and with str.rsplit() you can split multiple times up to a limit, again from the end:

>>> 'foo:bar:baz:spam:eggs'.rsplit(':', 3)
['foo:bar', 'baz', 'spam', 'eggs']

Last but not least, you could use str.rfind() to find just the index of a substring, searching from the end:

>>> 'foo:bar:baz'.rfind(':')
7

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.

Copied!

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.

Copied!

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
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().

Notice that we provided a value of 1 for the maxsplit argument because we only want to split the string once, from the right.

Copied!

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.

Copied!

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.

Copied!

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.

How do you split a string on the last occurrence of a character 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 .

How do you split a string after a specific character in Python?

Python String split() Method Syntax separator : This is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator. maxsplit : It is a number, which tells us to split the string into maximum of provided number of times.

How do I get the last substring in Python?

The last character of a string has index position -1. So, to get the last character from a string, pass -1 in the square brackets i.e. It returned a copy of the last character in the string. You can use it to check its content or print it etc.

How do you get all the items after a character in Python?

To get everything after the last slash in a string:.
Use the str. rsplit() method to split the string on a slash, from the right..
Get the list element at index 1 ..
The method will return a new string that only contains the part after the last slash..