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

David Hortensius
David Hortensius
3,898 Points

Creating a concatenated string for the regex input

Hello,

I'm trying to get my fingers around the word_length challenge and came up with the idea to make a string of my regex 'input', also thanks to this forum posts.

  reg_string = "r'\w{" + str(count) + ",}'"
  return re.findall(reg_string, string) 

But that doesn't work. If I check the forum I see:

  reg_string = r'\w{' + str(count) + ',}' 

And that works perfectly well. Though, I don't understand. Why do I on one instance need quotes at the beginning and end to concatenate a string variable and in this case using none is perfectly fine?

Hope you can help me with that.

1 Answer

I think that it's because in your example the r is inside the opening quote. The would mean that it's not a regular expression. The copy of code that you got from the forum is a regular expression because the r is outside the opening quote.