Search for a String in Text Files using enumerate()
We are just finding string is present in the file or not using the enumerate() in Python.
Python3
with open (r "myfile.txt" , 'r' ) as f: for index, line in enumerate (f): # search string if 'Line 3y' in line: print ( 'string found in a file' ) # don't look for next lines break print ( 'string does not exist in a file' ) |
Output:
string does not exist in a file
Python – How to search for a string in text files?
In this article, we are going to see how to search for a string in text files using Python
Example:
string = “GEEK FOR GEEKS”
Input: “FOR”
Output: Yes, FOR is present in the given string.
Text File for demonstration:
Contact Us