I have a file consists of many strings. Looks like
sdfsdf sdfsdfsdf sdfsdfsdf test gggg uff test test fffffffff sdgsdgsdgsdg sdgsdgsdgsdg uuuttt 5
You can use regular expressions:
import re with open('myfile.txt', 'r') as f: txt = f.read() cnt = len(re.findall(r'\btest\b', txt))
If you don't care about case sensitivity (also match Test or TEST)
Test
TEST
cnt = len(re.findall(r'\btest\b', txt, flags=re.I))