• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Using Python to Search File for String

Using Python to Search File for String

May 16, 2022 Leave a Comment

To search a file for a string using Python, you can use the read() function and use the Python in operator to check each line for a particular string.

string = "word"
in_file = False

with open("example.txt","r") as f:
    if string in f.read():
        in_file = True

print(in_file)

#Output:
True

When working with files in Python, the ability to easily search files for specific text can be valuable.

To search a file for a string in Python, we can use the Python read() function to read the entire file and use the Python in operator to check if a string is in the file.

Below is a simple example of how you can search a file for a string using Python.

string = "word"
in_file = False

with open("example.txt","r") as f:
    if string in f.read():
        in_file = True

print(in_file)

#Output:
True

How to Search File for String and Return Location in Python

If you want to search a file for a string and return the line number and line position, we can make a slight adjustment to our code from above.

First, we need to loop over all lines and keep track of the line number.

Then, we can use the Python find() function to find the position of first occurrence of a string in each line.

Below is an example showing how you can search a file for a string and return where the string was found in Python.

string = "word"
line_count = 0

with open("example.txt","r") as f:
    for line in f:
        if line.find(string) > 0:
            print(line_count, line.find(string))
        line_count = line_count + 1

#Output:
1 20

Hopefully this article has been useful for you to learn how to search a file for a string using Python.

Other Articles You'll Also Like:

  • 1.  Get Current URL with Selenium in Python
  • 2.  Using Python to Split String by Tab
  • 3.  Break Out of Function in Python with return Statement
  • 4.  Calculate Compound Interest in Python
  • 5.  Apply Function to All Elements in List in Python
  • 6.  pandas Duplicated – Find Duplicate Rows in DataFrame or Series
  • 7.  Deque Peek and Queue Peek Functions in Python
  • 8.  Sorting a List of Tuples by Second Element in Python
  • 9.  Cartesian Product of Two Lists in Python
  • 10.  Replace Values in Dictionary in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy