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

Abhishek Gangadhar
Abhishek Gangadhar
9,467 Points

Not sure whats wrong with my code

Its say that an empty list is being returned. not sure why...

word_length.py
import re

# EXAMPLE:
# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']
def find_words(count,string):
  match = re.findall(r"\w{count,}",string)
  return match

1 Answer

Keith Whatling
Keith Whatling
17,752 Points

Hi,

Your expression is 100% correct, however you will need to take the variable 'count' and put it in your code.

At the moment count is just a word in the string and not the variable you are passing. Try the following.

re.findall(r'\w{' + str(count) + '}', string)

I would love to know how to use the curly braces with string.format(count), if you find out then please ping me. Maybe we should ask Kenneth to shine some light on that one.

Josh Keenan
Josh Keenan
19,652 Points

Tagging Kenneth Love so maybe he can help you guys out :)

Abhishek Gangadhar
Abhishek Gangadhar
9,467 Points

lol ya silly mistake :) re.findall(r'\w{' + str(count) + ',}', string)

we also need to use the ',' (comma) cause we need all strings with 6 or more letters

Iskander Ismagilov
Iskander Ismagilov
13,298 Points
def find_words(count, string):
  q = '{'
  w = ',}'
  print(re.findall(r'\w{}{}{}'.format(q, count, w), string))