-3
import re
def find_words(num,string):
    a = re.findall('\w{int(num),}',string)
    return(a)
find_words(4, "dog, cat, baby, balloon, me")

The output is []

#####
import re
string = "dog, cat, baby, balloon, me"
a = re.findall('\w{4,}',string)
print(a)

The output is ['baby', 'balloon']

Dez
  • 21
  • 2

1 Answers1

1

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.

MarredCheese
  • 17,541
  • 8
  • 92
  • 91