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

Mohsen Qaddoura
Mohsen Qaddoura
22,237 Points

Python Regular Expression code challenge word length function find_words(count, str) returns an empty list

Here is my code:

import re

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

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

returns an empty list. I checked my regex in the return statement using directly the value of "4" instead of variable count and got the exact output of the example.

word_length.py
import re

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

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

1 Answer

Thomas Kirk
Thomas Kirk
5,246 Points

Hi,

You cant use your variable inside your regular expression string. You need to turn count into a string, then use string concatenation to slot it in between two parts:

count = str(count)

r'...' + count + '...',

Also, you do not need to include the word boundaries in your code.

Hope that helps!

Mohsen Qaddoura
Mohsen Qaddoura
22,237 Points

Thank you. It did help. Wish you the best in your career.