Reputation: 191
I have a function which takes a count and a string as input. It should return a list of all words in that string of length count, and greater. Python however doesn't recognise my variable and returns an empty list.
def word_number(count, string):
return re.findall(r'\w{count,}', string)
How do I pass in the variable 'count' so that the function returns words of count and longer?
Upvotes: 3
Views: 162
Reputation: 11134
You can use str.format to achieve your goal.
def word_number(count, string):
return re.findall(r'\w{{{0},}}'.format(count), string)
Upvotes: 2
Reputation: 42017
You can use printf
style formatting:
re.findall(r'\w{%s,}' % count, string)
Upvotes: 4