• 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
  • Ruby
  • 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.  Python Even or Odd – Check if Number is Even or Odd Using % Operator
  • 2.  How to Divide Two Numbers in Python
  • 3.  Python asinh – Find Hyperbolic Arcsine of Number Using math.asinh()
  • 4.  Pythagorean Theorem in Python – Calculating Length of Triangle Sides
  • 5.  pandas mode – Find Mode of Series or Columns in DataFrame
  • 6.  Using Python to Find Second Smallest Value in List
  • 7.  Get Name of Function in Python
  • 8.  Run Something Every 5 Seconds in Python
  • 9.  Draw Rectangle in Python Using turtle Module
  • 10.  Using Python to Remove Apostrophe From String

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy