Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to have the user input a word and once that happens I try to print the findall method with the variable as the pattern inside of the findall method. I didn't know if you are allowed to do this or not but when I run the program. I get an empty list as my result. I was just wondering if I could help with this. Here's my code:

import re


with open ('findall.txt', 'r') as f:
for word in f:
word = input("Enter the word to search for: ")
print(re.findall('word', f.read()))

What I have tried:

I tried looking online on how to use findall method, but I don't know that much about the re module so I could use some help.
Posted
Updated 7-Jun-21 3:00am
v2

in regards to the above can't you forget regex and just use count? i.e

Python
word = input("Enter the word to search for: ")
content = f.read()
total = content.count(word)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900