Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 26,999 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 26,999 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?