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

Dhruv Ghulati
Dhruv Ghulati
1,582 Points

Unsure about why I can't use {count, } to find a sequence of unicode characters longer than count.

Hi - what am I doing wrong here? Please also explain why I am theoretically wrong so I can improve for next time :)

Thanks!!

word_length.py
import re

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

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

2 Answers

Chase Marchione
Chase Marchione
155,055 Points

Hi there,

  • Inside of your function, you will want to convert count to string, so that it may be evaluated as such:
count = str(count)
  • Next up is a matter of using concatenation so that the count is properly considered as a variable in your return statement, which you are very close to doing. After apostrophes and concatenation are implemented accordingly:
  return re.findall(r'\w{'+ count + ',}', string)

Hope this helps.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Regex patterns are raw strings. They don't do anything special. They especially don't just read in variables and replace parts of themselves with said variables. CJ Marchione's answer is a good one.