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

word_length.py Please help. I think I'm close

Hello! Any advice on this would help. I'm getting the words in the list that are less than 6 still and I'm not sure why. Is this my only issue?

import re

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

def find_words(count, string):
  words_list = []
  for itb in string:
    words = re.findall(r'\D*', string)
    for word in words:
      if len(word) >= count:
        return word

3 Answers

gaetano amoroso
gaetano amoroso
2,993 Points

I just saw that I didn't answer exsatly at your question, so now I used findall for your solution

import re
def find_words(count, string):
    l = re.findall(r"\b\w{%s,}\b" % count, string) 
    new_l = []

    for word in l:
      if len(word[:len(word)]) >= count:
    return new_l

print(find_words(4, "dog, cat, baby, balloon, mes"))

the %s, stands for {from count, to infinite numbers}, it is the same sintax for print function when it have to farmatting a string

best regards

Sometimes the instructions are a bit difficult to interpret. What they want is a function more like this:

def find_words(count, data):
  return re.findall(r"\b\w{%s,}\b" % count, data)

Okay. So what exactly does the {%s,} mean?

gaetano amoroso
gaetano amoroso
2,993 Points

great job, you are almost.

def find_words(count, string):
    for word in word_list:
      if len(word) >= count:
    return l

print(find_words(4, "dog,cat,baby,balloon,me"))

Your problem was that the followingcode retrive a list with two element, the firsth is a fullstring and the second is a empty string. I believe that you want one element in list for each word in string between the commas

 words = re.findall(r'\D*', string)

I hope I was helpful

This was helpful. Do you know a way to do it using the: re.findall() function? I was hoping to practice this.

Also.. Is there a way to find out my output? As you did above with the print statement. I hate how I never get to see the output. I find that seeing how the output changes (even if I'm wrong) helps with the learning process.