Codewars 5 kyu Extract the domain name from a URL
https://www.codewars.com/kata/514a024011ea4fb54200004b/train/python
Task
Write a function that when given a URL as a string, parses out just the domain name and returns it as a string. For example:
- url = "http://github.com/carbonfive/raygun" -> domain name = "github"
- url = "http://www.zombie-bites.com" -> domain name = "zombie-bites"
- url = "https://www.cnet.com" -> domain name=”cnet”
Verbalization
replace “http://” to “”
replace “https://” to “”
Replace “www.“ to “”
Extract left side of “.”
idx = s.find(“.”) #find the index of “.”
r = s[:idx] # slice the index of “.”
Code
def domain_name(url):
name = url.replace("http://","").replace("https://","").replace("www.","")
idx = name.find(".")
name = name[:idx]
return name
Reference