How to convert a URL inside a string to a hyperlink in Python

1 Answer

0 votes
import re

def convert_urls_to_links(text):
    return re.sub(r"(https?://[^\s]+)", r'<a href="\1">\1</a>', text)

str_text = "This is my website check it out https://www.collectivesolver.com"
result = convert_urls_to_links(str_text)

print(result)
 



'''
run:

This is my website check it out <a href="https://www.collectivesolver.com">https://www.collectivesolver.com</a>

'''

 



answered May 2 by avibootz
...