The part of your function that is supposed to be replaced with the input variable num
won't work as posted because it's just a string. You have to replace that part of the string first, like this:
import re
def find_words(num, string):
target = '\w{%d,}' % num # if num = 4, target becomes '\w{4,}'
return re.findall(target, string)
print find_words(4, "dog, cat, baby, balloon, me") # prints ['baby', 'balloon']
Alternatively, you could do this for the same result:
target = '\w{{{},}}'.format(num)
The double brackets are needed when using format()
in order to include literal brackets in the string.