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

Rabih Atallah
Rabih Atallah
3,405 Points

wrong script

Please why is my regex script wrong. Im trying to return a list of words of 'nb characters' each from the string strg

word_length.py
import re

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

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

In your regex string "nb" is a two character string, not a variable. Try this as your regex string:

r'\w{{{0},}}'.format(nb)

Since format() uses curly braces as field markers, you use double-curlies {{ and }} to escape them. {0} is the field replaced by format()

Rabih Atallah
Rabih Atallah
3,405 Points

Thanks again! that was really helpful...