Issue
How can I find a string in a text file and then print which line that string was found on?
I am using Python 2.7.
Solution
In Python, enumerate
is handy for this sort of thing:
with open(myfile) as f:
for index, line in enumerate(f, 1):
if s in line:
print("'{0}' found on line {1}".format(s, index))
Answered By – jonrsharpe
Answer Checked By – Terry (BugsFixing Volunteer)