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

Why doesn't get this the words with four or more characters? (using '\w{4,}' in this instance

Using regular expressions. Could it be that the input variable does net get parsed like this?

word_length.py
import re

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

def find_words(ncount, nstring):
  fmatch = re.findall(r"\w{ncount,}", nstring);
  return fmatch

2 Answers

William Gough
William Gough
18,852 Points

I used the code posted above:

def find_words(ncount, nstring):
  fmatch = re.findall(r"\w{" + ncount + r",}", nstring);
  return fmatch

It gave me an error saying: Cannot convert integer to string, this solution worked for me:

def find_words(ncount, nstring):
  fmatch = re.findall(r"\w{" + str(ncount) + r",}", nstring);
  return fmatch

Ahh yes, forgot about the type. Good catch.

Correct, the parameter ncount is being treated as a literal string within the raw text string used for the regex.

You have a couple of options, first, you can concatenate the variable in between some strings to build the regex:

def find_words(ncount, nstring):
  fmatch = re.findall(r"\w{" + ncount + r",}", nstring);
  return fmatch

Second, you can use .format() and escape the curly braces (this looks a bit insane):

def find_words(ncount, nstring):
  fmatch = re.findall(r"\w{{{},}}".format(ncount), nstring);
  return fmatch

In a string where you're using .format(), you use double curly braces to print a literal curly brace:

print("A string with {{curly braces}}".format())
a_variable = "a variable"
print("A string with {{curly braces and {} too}}".format(a_variable))

The above will print:

A string with {curly braces}
A string with {curly braces and a variable too}