Extract URL from a string in Python

In this tutorial, we are going to learn how to extract URL from a string in Python. We are going to enter any string and check whether the string has any URL or not. If they had any URL then print the URL.  This kind of problem can be solved by using certain methods. the methods are:-

using a regular expression

We will use the concept of Regular Expression in Python to solve the problem.

import re
def find(URL):
  url = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[[email protected]&+]|[!*\(\), ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',URL) 
  return url 
URL ='I am a blogger at'
print("searched url: ", find(URL))

In the above code, the string is scanning from left to right and the matches are returning in order.



searched url: ['']

using string slicing method

here the URL ‘<a href=”″>’ found in a string. the starting character found at the 9th index and the last char found at the -2(negative indexing)position. So that we can be using string slice we can extract the URL from a string.

html_string = '<a href="">'
url = html_string[9:-2:1]

The extracted URL from the string is,


<a href="">

Leave a Reply

Your email address will not be published. Required fields are marked *