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

Dezhi Zhu
Dezhi Zhu
2,662 Points

Please help me! I am so confused

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.

word_length.py
import re

# EXAMPLE:
# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']
def find_words(num,string):
    a = re.findall('\w{int(num),}',string)
    return(a)

1 Answer

Steven Parker
Steven Parker
230,970 Points

Here's a couple of hints:

  • if you put the word "num" inside a string (with or without "int()" around it), it does not represent a value
  • you can convert a number to a string and then use concatenation to join it with other strings
Dezhi Zhu
Dezhi Zhu
2,662 Points

Hi Steven Thanks so much for help. There is one more thing I am quite confused.

a = re.findall('\w{{},}'.format(num),string)

Why this still not work? Where and how should I put curly bracket?

Steven Parker
Steven Parker
230,970 Points

When using .format, if you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.