Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Better way of replacing subtring from a string

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading