Remove Part of String Before the Last Forward Slash

后端 未结 6 1938
终归单人心
终归单人心 2021-02-05 19:04

The program I am currently working on retrieves URLs from a website and puts them into a list. What I want to get is the last section of the URL.

So, if the first elemen

6条回答
  •  北海茫月
    2021-02-05 19:52

    Just use string.split:

    url = "/some/url/with/a/file.html"
    
    print url.split("/")[-1]
    
    # Result should be "file.html"
    

    split gives you an array of strings that were separated by "/". The [-1] gives you the last element in the array, which is what you want.

提交回复
热议问题