pythonurldiscord.pylast.fm

Split URL in Python


So, I have this URL: https://www.last.fm/music/Limp+Bizkit/Significant+Other

I want to split it, to only keep the Limp+Bizkit and Significant+Other part of the URL. These are variables, and can be different each time. These are needed to create a new URL (which I know how to do).

I want the Limp+Bizkit and Significant+Other to be two different variables. How do I do this?


Solution

  • You can replace https://www.last.fm/music/ in the URL to just get Limp+Bizkit/Significant+Other. Then you can split it in half at the / character to break it into two strings. Then the URL will be a list and you can access the indices with url[0] and url[1]:

    >>> url = "https://www.last.fm/music/Limp+Bizkit/Significant+Other"
    >>> url = url.replace("https://www.last.fm/music/",'').split('/')
    >>> first_value = url[0]
    >>> second_value = url[1]
    >>> first_value
    'Limp+Bizkit'
    >>> second_value
    'Significant+Other'