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

Not sure what I'm doing wrong

The instructions: Create a function named find_words that takes a count and a string. Return a list of all of the words in the string that are count word characters long or longer.

I know num is becoming part of the string, but I'm not sure how else to do this.

word_length.py
import re

def find_words (num, my_string):
    my_list= []
    my_list.append(re.findall(r'\w{num,}', my_string))
    return my_list
# EXAMPLE:
# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

There are two issues.

  • num Needs to be formatted into the regex. Currently it's just the characters "num"

  • re.findall() returns a list so it doesn't need to be appended into another list. Simply assign the results to my_list

Shouldn't

word_list = (re.findall(r'\w{{},}'.format(count), string))

work?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Michael, format can be confused by the regex braces vs the format placeholders. The regex braces need to escaped by doubling them:

r'\w{{{},}}'.format(count)

which can become hard to read.

Thanks, Chris!