How can I extract whatever follows the last slash in a URL in Python? For example, these URLs should return the following: Show
I've tried urlparse, but that gives me the full path filename, such as
Remi Guan 20.5k17 gold badges62 silver badges83 bronze badges asked Aug 31, 2011 at 7:23
5 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:
So you can get the part you're interested in simply with:
Remi Guan 20.5k17 gold badges62 silver badges83 bronze badges answered Aug 31, 2011 at 7:28
Luke404Luke404 9,8423 gold badges24 silver badges31 bronze badges 6 One more (idio(ma)tic) way:
answered Aug 31, 2011 at 7:31
KimvaisKimvais 37.2k16 gold badges106 silver badges140 bronze badges 2
answered Aug 31, 2011 at 7:28
0 You can do like this:
Where tail will be your file name.
answered Sep 20, 2013 at 13:53
neowinstonneowinston 7,41610 gold badges51 silver badges83 bronze badges 1 urlparse is fine to use if you want to (say, to get rid of any query string parameters).
Output:
answered Apr 4, 2013 at 5:51
Jacob WanJacob Wan 2,33321 silver badges19 bronze badges 2
answered Jan 15, 2019 at 5:01
RochanRochan 1,2841 gold badge13 silver badges15 bronze badges 2 Here's a more general, regex way of doing this:
answered Apr 12, 2018 at 14:32
sandoronodisandoronodi 3162 silver badges11 bronze badges 1 First extract the path element from the URL:
and then you can extract the last segment with string functions:
(example
resulting to answered Sep 19, 2011 at 9:22
tzottzot 89k29 gold badges136 silver badges200 bronze badges 2 Use
Note:
if your url ends with a
answered Nov 18, 2020 at 22:01
The following solution, which uses
yields:
answered Dec 22, 2021 at 9:32
answered Aug 31, 2011 at 7:28
fardjadfardjad 19.4k6 gold badges50 silver badges67 bronze badges 0 Split the url and pop the last element answered May 19, 2017 at 9:16
Atul YadavAtul Yadav 1,9721 gold badge13 silver badges15 bronze badges Split the URL and pop the last element
answered Jun 10, 2021 at 8:58
Jaimin PatelJaimin Patel 4,4113 gold badges31 silver badges35 bronze badges
Output:
sigod 2,3712 gold badges24 silver badges42 bronze badges answered Feb 18, 2013 at 21:42
live_alonelive_alone 1631 silver badge11 bronze badges 1 How do you get everything after the last slash in Python?To get everything after the last slash in a string:. Use the str. rsplit() method to split the string on a slash, from the right.. Get the list element at index 1 .. The method will return a new string that only contains the part after the last slash.. How do you get the string after the last slash?First, find the last index of ('/') using . lastIndexOf(str) method. Use the . substring() method to get the access the string after last slash.
How do I get the last substring in Python?The last character of a string has index position -1. So, to get the last character from a string, pass -1 in the square brackets i.e. It returned a copy of the last character in the string. You can use it to check its content or print it etc.
How do you split a string with a forward slash in Python?Use the str. split() method to split a string on the forward slashes, e.g. my_list = my_str. split('/') .
|