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

Brian McGary
Brian McGary
9,289 Points

Simpler Solution to find_words()?

I just finished the find_words() coding task and even though I got a working solution was wondering if there is a simpler one. Also, I would like to know why building out the regular expression string was needed and I could not simply plug the variable into the return.

Here is my code:

def find_words(my_count, my_str):
  regex_str = r'\w{'+str(my_count)+',}'
  return re.findall(regex_str, my_str)

[MOD: added ```python markdown formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Your solution is about as simple as it gets. Alternative solutions use a different formatting process to construct the string, but the end result is the same.

As for including the regex in the return statement, a modified version of your solution passes the challenge:

import re
def find_words(my_count, my_str):
    return re.findall(r'\w{'+str(my_count)+',}', my_str)

You can peruse other solutions to this challenge here