Editor

Extract All Hashtags

Intermediate · Write the code · from Python Regular Expressions: Pattern Matching from Basics to Advanced

From the lesson: Python Regular Expressions: Pattern Matching from Basics to Advanced

Imagine you're working at a help desk and need to find every email address buried in thousands of support tickets. You could scan each line character by character, but that would take forever. Regular expressions let you describe the pattern of an email address, and Python finds every match for you.

Read the lesson

Write a function extract_hashtags(text) that returns a list of all hashtags in the text. A hashtag starts with # followed by one or more word characters (letters, digits, or underscores). Return just the tag names without the # symbol.

import re
def extract_hashtags(text):
# Use re.findall() with a group to capture the tag name
pass
result = extract_hashtags('Love #Python and #coding! #100DaysOfCode')
print(result)

More Regex exercises

All Python exercises · 50 practice problems · Standard Library Essentials quiz