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

Rabih Atallah
Rabih Atallah
3,405 Points

challenge problem

PLease how can I fix my code? help is much appreciated

word_length.py
import re

# EXAMPLE:
# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']
def find_words(nb, string2):
  string2.split()
  L = []
  for e in string2.split():
    if len(e) >= nb:
      L.append(e)
  return L

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

First hint is some of the items in the string are adjacent to a comma, so splitting with the default split() leaves words like "match,"

Second hint is this is an re challenge so you'll likely need to use an 're.search' or 're.match' to find the correct solution to look for words of nb or longer actual characters: r'\w'*6

Bonus, first string2.split() isn't assigned to a variable so it's output is thrown away. The line could be deleted.

Rabih Atallah
Rabih Atallah
3,405 Points

Thank you again Chris! It was very helpful I really appreciate it