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...please help I am stuck

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 have tried string concatenation, string format but I am back to where I started. I can't seem to be able to format count so that it is picked as a variable to enable me to get the expected output...words with letters equal to or more than count. Please help.

import re

# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']
def find_words(count, string):
    return re.findall(r'\w {count,}', string)

2 Answers

Tom Sager
Tom Sager
18,987 Points

You need to get the variable 'count' to be evaluated inside the string that is used for the regular expression. Try this:

"\w{%s,}" % count

This is string interpolation. The %s is a place holder, and % count provides the value to be inserted. So if count = 4, the result is "\w{4,}". The %s says the value should be evaluated as a string. There are other options, such as %d (decimal) and %f (float).

Then you can use

re.findall(r"\w{%s,}" % count, words)

I hope this helps!

Yes it does, thanks a lot for explaining in detail. Let me try working on my challenge now. Thanks again.


Thanks Tom, I encountered the '%' in dates and time formatting but it didn't occur to me that's the only type of formatting I didn't try in this case. '%' is really a powerful tool.