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 trialGordon Reeder
2,521 PointsRegular expressions code challenge. Help.
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.
It won't pass and I can't see what I'm doing wrong.
import re
def find_words(a_count, a_string):
return re.findall(r'\w{a_count,}', a_string)
# EXAMPLE:
# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']
2 Answers
Dan Johnson
40,533 PointsYou'll have to use format or a similar method to do string interpolation:
Format:
regex = r"\w{{{},}}".format(count)
Old method as it's easier to read:
regex = r"\w{%d,}" % count
Brian Saunders
5,780 PointsI'm not sure why I didn't think of this myself. That totally works! Thanks.
Gordon Reeder
2,521 PointsGordon Reeder
2,521 PointsThanks Dan, that worked (first example). I'm not exactly sure why.
Dan Johnson
40,533 PointsDan Johnson
40,533 PointsThe first method looks odd since format uses {} for replacement. So you have to add extras to escape them to place the literal pair.