Better way of replacing subtring from a string

Question:

I’m new to python and looking for a better or faster solution.
I replaced substring from string using this solution.

In this example, that is to replace https://www.site1.com// from a url string

site2 = 'https://www.site2.com/'
url = 'https://www.site1.com//vehicles/2023/Ford/F-150/Calgary/AB/57506598/?sale_class=New'

output = site2 + url[url.index('vehicles/'):] 
print(output) # https://www.site2.com/vehicles/2023/Ford/F-150/Calgary/AB/57506598/?sale_class=N

Is this a faster solution? Is there a better one? what about using regex perhaps?

Asked By: alphaulos

||

Answers:

When manipulating structures such as URLs it’s better to use tools designed for the purpose rather than treating them as strings.

In this case you want to change the host (netloc) to www.site2.com

So…

from urllib.parse import urlparse, urlunparse

new_site = 'www.site2.com'
url = 'https://www.site1.com//vehicles/2023/Ford/F-150/Calgary/AB/57506598/?sale_class=New'
scheme, _, path, params, query, fragment = urlparse(url)
new_url = urlunparse((scheme, new_site, path.replace('//', '/'), params, query, fragment))
print(new_url)

Output:

https://www.site2.com/vehicles/2023/Ford/F-150/Calgary/AB/57506598/?sale_class=New
Answered By: DarkKnight
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.