python strip html tags like strip_tags in php

Earlier this week I needed to remove some HTML tags from a text, the target string was already recorded with HTML tags in the database and one of the requirement specifies that in some specific page we need to render it as a raw text.

I knew from the beginning that regular expressions could apply for that challenge, but since I am not an expert with regular expressions I looked for some advise in stack overflow and then I found what I actually needed.

Below is the function I have defined:

def remove_html_tags(text):
"""Remove html tags from a string"""
import re
clean = re.compile('<.*?>')
return re.sub(clean, '', text)

Also published on Medium.

Leave a Reply

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