Posts

Showing posts with the label python strings

How to search for a string in text files in Python?

Image
  In this article, we are going to see how to search for a particular string in a text file.  Ex:- We have a text file and we want to search a particular string from that file with different methods. Consider we have text file with some text bellow:-  " This is Codin India.  A Coding Platform for programmers   You can learn programming from this portal   We are expert in Python and every day we come with tips and tricks   This Channel is owned by Digamber Jha  "   We can see different method one by one. Lets start with first method. Method 1:-  First We will just find the string is present in the file or not.  If the string found then we will print  the output. Steps for searching:- Open a file. Read the file and store it in a variable. Check the condition using If statement. If the condition is true print "String found" else "String not found". Close the file. Output:- This is simple method for finding the string or text from file but has some limitations