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?
您可以使用printf
样式格式:
re.findall(r'\w{%s,}' % count, string)
You can use str.format to achieve your goal.
def word_number(count, string):
return re.findall(r'\w{{{0},}}'.format(count), string)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.