How to get the last part of a URL after the last slash (/) in Python

3 Answers

0 votes
url = "http://www.website.com/abc/xyz"
   
s = url.rsplit('/', 1)[-1]
 
print(s)  
 
  
  
'''
run:
  
xyz
  
'''

 



answered Feb 17, 2020 by avibootz
0 votes
url = "http://www.website.com/abc/xyz"
   
s = url.rsplit('/', 1).pop() 
 
print(s)  
 
  
  
'''
run:
  
xyz
  
'''

 



answered Feb 17, 2020 by avibootz
0 votes
url = "http://www.website.com/abc/xyz"
   
s = url.split("/")[-1]
 
print(s)  
 
  
  
'''
run:
  
xyz
  
'''

 



answered Feb 17, 2020 by avibootz

Related questions

1 answer 230 views
2 answers 323 views
2 answers 266 views
3 answers 892 views
1 answer 224 views
1 answer 206 views
...