How to get everything after last slash in a URL? How to get everything after last slash in a URL? python python

How to get everything after last slash in a URL?


You don't need fancy things, just see the string methods in the standard library and you can easily split your url between 'filename' part and the rest:

url.rsplit('/', 1)

So you can get the part you're interested in simply with:

url.rsplit('/', 1)[-1]


One more (idio(ma)tic) way:

URL.split("/")[-1]


rsplit should be up to the task:

In [1]: 'http://www.test.com/page/TEST2'.rsplit('/', 1)[1]Out[1]: 'TEST2'