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

Vidhya Sagar
Vidhya Sagar
1,568 Points

What am i doing wrong ?:(:(

I get output as [] .Can someone explain what i am doing wrong.

word_length.py
import re

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

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Without giving the exact solution, you are very close. Three things to fix:

  • your regex is looking for the exact three characters "num" and is not substituting the variable value. Use string formatting to get the value of the variable into the regex.
  • the regex is looking for a leading space before the word. This isn't necessary and it causes the first item to never match since it has no leading space. Some for leading comma: not necessary. Try wrapping the word with word boundary markers: \b
  • need comma after num value to get the "or longer" aspect of the challenge.