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 challenge

im stuck help me out

word_length.py
import re

def find_words(count, string):
    return re.findall(r'\w', string)

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

4 Answers

Hey! this took me about 20 friggin minutes of head-banging to figure out:

def find_words(c, string): return re.findall(r'\w+' * c, string)

I've seen a LOT of answers that involve crazy amounts of curly braces, which we haven't learned yet in this course. helpful?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Regex patterns provide many ways to get the correct result. I had not seen your solution to this code challenge before. Nicely done!

Your pattern may be a bit confusing to some as reads as “one or more word characters followed by one or more word characters followed by one or more word characters“. This still works due to pattern backtracking but it is less efficient.

A better presentation might be

re.findall(r'\w'*c + '+', string)

This pattern reads as “one word character followed by one word character followed by one or more word characters“.

Edit: replaced 3 with c

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

You are close. What you have now finds all of the strings that contain a single word-character.

You need to modify the matching pattern to find count number of \w characters or more. For example, to find 3 or more characters you could use.

 re.findall(r'\w{3,}', string)

How would find "count or more" characters?

Post back if you need more help. Good Luck!!

still can't get it, here is the question 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'm confused by the *3 on this. We need to be able to search for the length supplied to the function. Would *c work as well?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Sorry, fixed example code replacing 3 with variable c