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

introduction to regular expressions

i cant seem to get this right....been stuck for more than 24 hours....please help

here is the question of it

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.

word_length.py
import re

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

3 Answers

Thomas Kirk
Thomas Kirk
5,246 Points

Hi,

The question asks you to return words with a certain number of letters or more, so you are going to have to put that number into your code, which at the moment you don't do. For example, if we were looking for words with 4 or more letters, we might use:

return re.findall(r'\w{4, }, string)'

But instead of 4 letters or more, you need 'count' letters or more. To do that, you can use string concatenation to work your 'count' argument in.

To do so, first you need to turn count into a string. Your current code is turning count into an empty string, instead use:

count = str(count)

Then concatenate:

return re.findall( ... " + count + " ...

Also, the re.findall in your current code is looking at your count argument for the words/letters, you need to be looking at the string argument...

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

The task is to find all of the words in string that are count number of characters or longer. So if count is 3, find all of the words that are three letters or more long.

What you're doing right now is looking for any number of word characters, a comma, and then 1+ word characters in an empty string.

Dhruv Ghulati
Dhruv Ghulati
1,582 Points

I tried :

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

And get the error:

Bummer! Didn't get the right output. Output was []. Length was 6.

I guess my error is something to do with the fact I need it to return a list?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

"count" is just those letters inside of a regular expression. You have to get the variable's value in there somehow.