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

How do i put a variable into a raw string?

I need to find all the words, with a given length (num) or longer, in a string (str) using a regex. I can get it work with a hard coded number like this re.findall(r'\w{4,}', str) but how can I use a variable so i can have different lengths?

I've tried this re.findall(r'\w{num,}', str) and re.findall(r'\w{%num,}', str) but I get an empty list back.

word_length.py
import re

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

def find_words(c, s):
    return re.findall(r'\w{c,}', s)

5 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Other ways to embed variable values into raw strings:

>>> import re
>>> c = 5
>>> s = "this and that alrighty to booth"
>>> re.findall(r'\w{' + str(c) + ',}', s)
['alrighty', 'booth']
>>> re.findall(r'\w{%s,}' % c, s)
['alrighty', 'booth']
>>> re.findall(r'\w{{{},}}'.format(c), s)
['alrighty', 'booth']
>>> re.findall(r'\w{{{c},}}'.format(c=c), s)
['alrighty', 'booth']
>>> re.findall(fr'\w{{{c},}}', s)
['alrighty', 'booth']
>>> 

Post back if you need more help. Good luck!!

Mark Sebeck
Mark Sebeck
Treehouse Moderator 37,799 Points

Thanks Chris! I knew there had to be a cleaner way

Hi Chris,

I have two questions about the code that you posted.

  1. Why do we need + for the code below?

    re.findall(r'\w{' + str(c) + ',}', s)
    
  2. Why do we need three sets of curly braces instead of two for the code below?

    re.findall(r'\w{{{},}}'.format(c), s)
    
Mark Sebeck
Mark Sebeck
Treehouse Moderator 37,799 Points

Hopefully Chris doesn't mind me jumping in here. I retook the regular expression class and have studied Chris's solutions after the last time I answered this question.

On number 1 the '+' are because we are simply concatenating a string. Just like "My name is " + name + "."

Number 2 is more complicated. So for the findall we want

 re.findall(r'\w{c,}', s)

Where the c will be replaced with our variable using format.

To get those brackets we need to double the brackets because that's how you escape them to insert them into a string. But then we want to replace the c with whats in the format string so we need another set of brackets replacing the c.

Hope that clears it up.

Feel free to jump in Chris if you can explain it clearer.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Thanks Mark Sebeck! I answered a similar question yesterday regarding the concatenation.

First, look at the whole regex:

r"\w{" + str(count) + r",}"
# 3 parts 
r"\w{"  # 1
str(count)  #2
r",}"  #3

The first and last are raw strings. The second creates a string from the variable count. Using the + concatenates the three strings into one string.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points
re.findall(r'\w{{{},}}'.format(c), s)
               xxff xx
# x marks escaped pair that will be replaced 
#    by single curly bracket for use in regex
# f marks curly bracket used by format that
#    will be replaced by substitution
Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 37,799 Points

I'm sure there is a better way to do this but this did pass the challenge.

    return re.findall(r'\w{' + str(c) + ',}', s)
Mark Sebeck
Mark Sebeck
Treehouse Moderator 37,799 Points

I'm actually going to be retaking this course soon because I am reviewing Python. I'll let you know if I find something better.

OK, thank you for the response! i didn't realize i could cast a string inside the raw string.

Mark Sebeck
Mark Sebeck
Treehouse Moderator 37,799 Points

You're welcome. If you find a cleaner way let me know. Even though it works I'm not sure it's the best way.

Thanks, Mark and Chris!

re.findall(r'\w{{{},}}'.format(c), s)

so, for the code above, out of the three sets of {}, the second set is to escape the innermost one(which is for the '.format method) and the comma, right?

ok, got it!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points
re.findall(r'\w{{{},}}'.format(c), s)
               xxff xx
# x marks escaped pair that will be replaced 
#    by single curly bracket for use in regex
# f marks curly bracket used by format that
#    will be replaced by substitution

Thanks!! got it!!!