Remove the string after the # mark in a website url

We are supposed to start a new project today but because I still need a few more days to prepare for it, therefore, I have posted another question and answer session in this website just to get the thing going. This question again comes from the codewars website and it goes like this.

Given a url, stripes everything after the # mark. The solution to this question is fairly simple. If that url contains the # mark, the program will remove all the characters after the # mark and returns the new url or else the original url will be returned.

def remove_url_anchor(url):
    index = url.find('#')
    if(index != -1):
        return url[0:url.find('#')]
        return url

We will continue to solve more python questions until the tutorial’s material for the new project is fully ready.

Leave a Reply

2 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
2 Comment authors
Paulo AlmeidaThere are some simpler ways Recent comment authors
newest oldest most voted
Notify of
There are some simpler ways
There are some simpler ways
Paulo Almeida
Paulo Almeida

You could also do:

from urllib.parse import urldefrag