Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Regular Expressions in Python Introduction to Regular Expressions Word Length

Tejas Wate
Tejas Wate
164 Points

word length

I don't understand why my code is incorrect

import re
def find_words(count,stri):
    return re.findall(r'\w{count,}',stri)

# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']

1 Answer

Jeff Muday
Jeff Muday
Treehouse Moderator 28,716 Points

You are very close. The word 'count' is taken as a literal rather than its value.

There are other ways to do this (re.compile), but a simple approach below will work too.

Best of luck with your Python journey!

import re
def find_words(count,stri):
    return re.findall(r'\w{' + str(count) + ',}',stri)