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 trialRohit Gopalan
81,945 PointsFinding all the words with length at least count word characters in a string
I used the following expression re.findall(r'\w{count,}',string) to find all the words with length at least count characters and was unable to get the code working. Can someone please suggest how I can overcome this issue?
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
Chase Marchione
155,055 PointsHi Rohit,
You might try converting the count variable to a string in the middle of the argument (and using the concatenation operator so that it works syntactically... in other words, so that your variable isn't interpreted as literal characters in the regular expression):
return re.findall(r'\w{' + str(count) + ',}',string)
Hope this helps!
Michael Vandenburg
6,952 PointsThe pink in the challenges text editor is a clue that it's reading the word "count" as part of the string being used as the regular expression. For the Python interpreter to see the variable and substitute its value when executing the code, it needs to be outside the quotation marks.
I don't think .format()
works because of the r
(at least I couldn't get it to work), but you can still concatenate the strings you need to build the complete expression.