-3

I have URL, e.g. google.com/page1/page2/abc123 and my output should be abc123.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83

2 Answers2

0

You could try to split the url address into a list and then get the last element of that list which would be what you need.

url = "google.com/page1/page2/abc123"
splitted_url = url.split("/")
last_element = splitted_url[-1]
print(last_element)
Marcin
  • 302
  • 3
  • 11
0

You have several options. One would be to split the URL into tokens based on '/' then take the last token in the returned list. Another option would be to find the last occurrence of '/' in the string then slice the string accordingly.

URL = 'google.com/page1/page2/abc123'

print(URL.split('/')[-1])
print(URL[URL.rfind('/')+1:])
DarkKnight
  • 19,739
  • 3
  • 6
  • 22