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

Maba Bah
Maba Bah
2,744 Points

How would I return a list of the words?

I understand this would return a "matchobject". How can I just get the words and return them in to a list?

word_length.py
import re

# EXAMPLE:
# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']

def find_words(count, string):
    words = re.findall(r'\w{count,}', string)
    return words

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

The other issue (and why you have to use str(count) is the regular expression is not a "normal string" but a RAW string. So the presence of a \ character signals in a regular string to interpret it in an escape sequence like newline \n or other escape sequences. Thus the typical "coeercion" may not work in the way you expect.

Here's another solution which would work too. Basically uses a %s which marks a string substitution of count which is intrepreted as a string.

def find_words(count, mystring):
    matches = re.findall(r'\w{%s,}' % count, mystring)
    return matches
Maba Bah
Maba Bah
2,744 Points

Thank you so much for clarifying! The above example is much easier to digest!

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Conceptually you got it correct, nice work! The regular expression can't interpret count as an integer-- so we have to take the regular expression "apart", and EMBED count as a string INSIDE the regular expression. See below.

Good luck with your Python journey. I have always found regular expressions to be powerful... But!!! one of the more difficult topics in programming.

def find_words(count, mystring):
    matches = re.findall(r'\w{' + str(count) + ',}', mystring)
    return matches
Maba Bah
Maba Bah
2,744 Points

It works! Although I understand how it works but not the why. What if I coerced "count" into an integer beforehand, for example "count = int(count)". Would I still be able to plug in "count" in the curly braces?