Best Practice to Extract and Remove URLs from Python String – Python Tutorial

By | August 13, 2019

In some comments, there ar some urls in them, if you want to remove them before displaying, you can read this tutorial. In this tutorial, we will introduce you on how to extract and remove urls from a python string.

python extract and remove urls

Import library

import re

Create a python string which contains some urls

text = 'My blog is and not'

Create a regular regression to match url


Match urls

match = re.findall(pattern, text)

Print urls


The output is:

[('', '', '', '', ''), ('', '', '', '', '')]

We have extracted urls from python string, then we will remove all of them.

Remove urls from python string

for m in match:
    url = m[0]
    text = text.replace(url, '')

Print result


The output is:

My blog is  and not

Leave a Reply

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