How To Get Everything After Last Slash In A Python String

Get everything after last slash in a string in Python

I use the built-in functions in Python to get everything after last slash in a python string. If you are interested, read this article in its entirety.

Get everything after last slash in a string

Use the str.rsplit() function

Syntax:

str.rsplit(separator, maxsplit)

Parameters:

  • separator: the value you want to round.
  • maxsplit: number of digits after the decimal point. The default is 0.

Example:

  • Initialize a string that contains a slash.
  • Use the str.rsplit() function to get everything after the last slash in a string.
myURL = 'https://learnshareit.com/how-to-sum-the-integers-from-1-to-n-in-python' 

# Use the str.rsplit() function to get everything after the last slash in a string
result = myURL.rsplit('/')[-1]

print('Get everything after last slash in a string:', result)

Output:

Get everything after last slash in a string: how-to-sum-the-integers-from-1-to-n-in-python

Use the split() function

Syntax:

str.split(sep, maxsplit)

Parameters:

  • sep: is the delimiter to split the original str string into small strings. If not specified, Python defaults sep to a blank character.
  • maxsplit: is the maximum number of splits. If not specified, Python defaults to an infinite number of splits.

Example:

  • Initialize a string that contains a slash.
  • Use the str.split() function to get everything after the last slash in a string.
myURL = 'https://learnshareit.com/how-to-sum-the-integers-from-1-to-n-in-python' 

# Use the str.split() function to get everything after last slash in a string
result = myURL.split("/")[-1]

print('Get everything after last slash in a string:', result)

Output:

Get everything after last slash in a string: how-to-sum-the-integers-from-1-to-n-in-python

Note: 

The usage of the maxsplit argument is the only difference between split() and rsplit(). If the maxsplit argument is set, the rsplit() function will split a string from the right side (from the last character), while the split() method will split from the left side (from the first character).

Use the re.sub() function

The Re.sub() method will replace all pattern matches in the string with something else passed in and return the modified string.

Syntax:

re.sub(pattern, replace, string, count)

Parameters:

  • pattern: is RegEx.
  • replace: is the replacement for the resulting string that matches the pattern.
  • string: is the string to match.
  • count: is the number of replacements. Python will treat this value as 0, match, and replace all qualified strings if left blank.

Example:

  • Import the re module.
  • Initialize a string that contains a slash.
  • Use the re.sub() function to get everything after the last slash in a string.
import re

myURL = 'https://learnshareit.com/how-to-sum-the-integers-from-1-to-n-in-python' 

# Use the re.sub function to get everything after last slash in a string
result = re.sub(r'^.+/([^/]+)$', r'\1', myURL)

print('Get everything after last slash in a string:', result)

Output:

Get everything after last slash in a string: how-to-sum-the-integers-from-1-to-n-in-python

Use the rpartition() function

Syntax:

string.rpartition(value)

Parameters:

  • value: The string you want to search for. Required parameter.

The rpartition() function returns a string and splits the string into three parts before the specified string, the specified string, and after the specified string.

Example:

  • Import the re module.
  • Initialize a string that contains a slash.
  • Use the rpartition() function to get everything after the last slash in a string.
import re

myURL = 'https://learnshareit.com/how-to-sum-the-integers-from-1-to-n-in-python' 

# Use the re.sub function to get everything after the last slash in a string
# Index 2 means getting the string after the specified string
result = myURL.rpartition('/')[2]

print('Get everything after last slash in a string:', result)

Output:

Get everything after last slash in a string: how-to-sum-the-integers-from-1-to-n-in-python

Summary

My article has ended. I hope this article can enrich your knowledge on how to get everything after last slash in a string in Python.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *