7 kyu Remove anchor from URL
https://www.codewars.com/kata/51f2b4448cadf20ed0000386/train/python
Complete the function/method so that it returns the url with anything after the anchor (#) removed.
Verbalization
- If there is no #, return url (use if "#" in url: )
- Search the index number of #. (use url.index(#))
- Select words from first index to one before index of # (use[0:i(ndex of #)])
Code
def remove_url_anchor(url):
if "#" in url:
return url[0:url.index("#")]
else:
return url
Reference
- How to remove all characters after a specific character in python?
https://stackoverflow.com/questions/904746/how-to-remove-all-characters-after-a-specific-character-in-python - Check if a word is in a string in Python
https://stackoverflow.com/questions/5319922/check-if-a-word-is-in-a-string-in-python
#other solution
def remove_url_anchor(url):
return url.split('#')[0]