Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Regular Expressions in Python Introduction to Regular Expressions Word Length

Chuck Anderson
Chuck Anderson
3,278 Points

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 67,736 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 29,715 Points

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

jaesonshin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
jaesonshin
Python Development Techdegree Graduate 9,939 Points

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 29,715 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 67,736 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 67,736 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 29,715 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 29,715 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.

Chuck Anderson
Chuck Anderson
3,278 Points

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 29,715 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.

jaesonshin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
jaesonshin
Python Development Techdegree Graduate 9,939 Points

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 67,736 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