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

Code Challenge Confusion

I am so incredibly lost in this code challenge. I was able to solve it via a work around (the error said that the count was 6, so I just input return re.findall(r'\w{6, }', string) and that solved it... BUT I want to do it the correct way.

I have looked all through the forums and other sources and concatenation seems to be the correct way to solve this challenge, but I am absolutely baffled as to how to do it that way. It is just going completely over my head. Please help me correct what I have and explain why and how concatenation is used to solve this.

Thanks.

word_length.py
import re

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

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

1 Answer

Thomas Kirk
Thomas Kirk
5,246 Points

Hi,

You basically have the correct answer, all you need to do is write the concatenation so that your variable, count, replaces 6 in the code that passed. Put everything left and right of the number 6 inside quotes ‘ ‘, then instead of 6, use ‘ + count + ‘

The reason concatenation is used is that inside the quotes, ‘count’ is no longer recognized as your variable. We turn the value of ‘count’ into a string, then slot that string into the rest of our string using concatenation, to complete the argument.

just figured it out after some tinkering..

import re

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