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 trialMUZ141004 Anna Makarudze
3,394 PointsWord 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
# EXAMPLE:
# >>> 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
18,987 PointsYou 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!
MUZ141004 Anna Makarudze
3,394 PointsThanks 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.
MUZ141004 Anna Makarudze
3,394 PointsMUZ141004 Anna Makarudze
3,394 PointsYes it does, thanks a lot for explaining in detail. Let me try working on my challenge now. Thanks again.
martin Kattam
Courses Plus Student 14,718 Pointsmartin Kattam
Courses Plus Student 14,718 PointsThanks.