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

Tobias Edwards
Tobias Edwards
14,458 Points

PYTHON: Introduction to Regular Expressions help

Question: 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.

I have attached my code which explains the rest of my situation through the commenting

My Question: Why doesn't this work?

Any help would be much appreciated. Thank you.

word_length.py
import re

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

# WHY WOULDN'T THIS WORK????
# I create a function find_words that takes a count and string as arguments
def find_words(count, string):
  # Then I TRY to return a list of all the words in 'string' that is made 
  # up of unicode letters that are at least count word characters long.
  return re.findall(r"\w{count,},", string)
  # OUTPUT: Didn't get the right output. Output was []. Length was 6.

4 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Because {count} in your regular expression isn' the same as, say {5} in a regular expression. The 'count' isn't going to be interpreted as a variable and replaced. In fact, it's just going to make an invalid expression, since 'count' isn't an acceptable argument for the regular expression counts operator.

You'll need to create your pattern so that the value of the count variable gets inserted into the pattern string. I'd suggest string concatenation.

Christos Peramatzis
Christos Peramatzis
16,428 Points

Kenneth what exactly do you mean by string concatenation? I thought string concatenation is if you do lets say: string1 = "Hello" string2 = "World" string1+string2 => "Hello World"

Tobias Edwards
Tobias Edwards
14,458 Points

I was just about to give up after not knowing what you meant, then it finally clicked XD Thank you!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Glad you powered through! Good work

You could also use .replace() on the string :) Personally I think that is a lot easier.

def find_words(count, string):
  return list(re.findall(r'\w{count,}'.replace('count', str(count)), string))
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You don't need to turn re.findall() into a list, it already is one.

And, yes, str.replace() is another way of approaching it but a) we haven't covered it; and b) I'm not sure it's really easier than concatenation.

Ah, okay. Guess you learn something new everyday :) And I believe that Python Basics is a prerequisite for the RegEx medallion, and that covers .replace()

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Uh, no, Python Basics definitely doesn't cover str.replace(). Here's my checking the script for it:

Python Basics script

Ah, okay. Guess I got it mixed up with something.. Really appreciate that you took the time to check the manuscript for me! Keep up the good work :)

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Wanna make a content person feel like they're going crazy? Tell 'em they talked about something they didn't :D

Can confirm :)

this is what I did but not yet working, any help, I'm still trying to solve it

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

Got it now, I did it, the last \w? i added there is not needed and only a "," could solve it