check string line python

def search_string_in_file(file_name, string_to_search):
    """Search for the given string in file and return lines containing that string,
    along with line numbers"""
    line_number = 0
    list_of_results = []
    # Open the file in read only mode
    with open(file_name, 'r') as read_obj:
        # Read all lines in the file one by one
        for line in read_obj:
            # For each line, check if line contains the string
            line_number += 1
            if string_to_search in line:
                # If yes, then add the line number & line as a tuple in the list
                list_of_results.append((line_number, line.rstrip()))
 
    # Return list of tuples containing line numbers and lines where string is found
    return list_of_results

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to check if a certain string is in a text file python how to detect if string appears in file python check for a string in a text file python check if value is in file python how to check if a string is a file python python check if a string is in a txt file if string or string in file python python checking for strings in a file check if a file contanes string python check if a file containes something python python check if string in text file check if string is in file pytohn check if a character is in filepython string if it exists in txt file python python check if file is text read file and check if string is in it python how to check if there is a string in a file in python check if string is in fiile python how to check whether a string is present in a file python see if a string is in a file python check if string is in a file python python check if file contains string in name check is a string is present in a file or not in python built in to check if a string in present in a file python check if a string in present in a file python check if string is present in file python how to check if string is in file python python check if a file has stuff in it how to find the file containing a string in python python check if substring in file name How to check if there is text in a file python python check if string is not in file python if string in file check for string in file python How to check if a string is present in a file python how to check if there is a string in a text file in python check if string in file name python python check if string containing ands is in a file python test if string is in file python check if file has text python test file exists python check in string in a file python read file and check if string contains if file contains string python python check file string python check string is in file how to check if word is in file python if string in file python check for a file in python how to check for something in a file python python check if string found in file how to check if a string exists in a file in python python check file for string python check if a file contains a string python check if string is in text file check to see if a txt file contains a string python check if text exists in file python check if text contains string file python check a file inside text document python check a file inside textdocument python find out file which contains word python check string in file python check if string in file python searching through file to find word python find lines in text file if found in python find lines in a text files via python check if text in file python find line on a txt file python how to loof for a substring in a file in python python search for a string in text file how to find values in a file in python python if string not in file print if file contains string print python python check for string in file check file for keyword python how to find out the line which contain number in strings in python file contains string python check if not in text file python py find file if character exists check if string is in file python if string in f.read() python command to check a substring in a file how to look for a string in another file python if string in txt pytjhom open a txt file in python and perform a find string python find in a word file how to search a matching string from file in python python find the line of a text in file python check if text file contains string python find text in file how to check exact line word in python python file contain find if a word is in file python python check if file includes string the best algorithm to find string in text file in python and return text not in file python how to find things in file in python how to determine what line a string is in a text file in python how to check if word in file python how to see if something is in a text file python python find string in lines how to find string in python file python check string in file from a file how to find if a specific word is in a line python check element in file python search text in txt files with python check the text in a given file in python how to check for a string in a file in python read a specific file containing a string in python python check content of file python if string not in file find text within txt file python python if string exists in file python check if string in file check if string exists in text file python python see if text file contains something python see if txt file contains something python script read a file and find string how to see if a string is in a text file in python how to tell what line texr is in a atext file python python search string from text file python check if text exists in file search for a string in a file in python search for a string in a file python how to find string in file using python how to find particular string in a file in python check for string in line of text file python how to find a specific string in a file python python if text in file find line string from file python find substring in a file python how to see if a string exists in a text file python if certain string in text file python if for text file in python file line.find python how to find a string in a text file using python PPYTHON IS STRING IN FILE find substring in a file python3 fine a string in a file python python check if file has string python how to find string exist in a file python if file contains string if text in file python check file text python check for a value in a file python search the line containing string in a file python how to check if a word is in a text file python python search string in file how to search within a a file with python finding a line in a file python if ' ' in txt: python how to find text inside a text file in python how to check if something is in a text file python how to search for specific text in a text file using python how to search files and print the lines python python check if string exists in file python if file contains substring how to search a file for a string in python find a string in a file python search for string in file in python python if string is in complete text file python search fror string in file python check if file contains string finding a specific text string through if else python for a file find text string in a file in python checking for specific text string from a file python finding something in a file python python if text file contains find line of a string in file python if pattern in line then print line using python how to check if a file contains a string in python find a string line in a python list file contains python search if a given string in present in a file python read file and search for string find keyword in python file python search for string in file and print line python find string in file check if a string exists in a file python find a substring in a file python python check if file contains character python find string in file and print line looking if someting is into file python python keep lines containing string in file find string in file python how rto search line by line in file in python how to check if a certain word is in a text file with python how to check if a line is in a txt file in python python check if string is in file python check if string exists in text file how to check if a string is in a text file python find a string in file python python search text file for line python search for a string in a file how to check if a specific text exists on txt file in python check if file contains a string python how to serch a matching string from a file in python how to serch a matching string from a file in py python find string from file how to search string in file using python find text in all lines txt python python check if a string is in a file find line with a string from a text file python how to check if something is in a file python searching for a string in a textfile python read file and find particluar strig how much in python how to check if a text file contains a string without loading it python python read file and find string python file string search word check which line in a file contains string python check if file cotains string python how to check if file is executable in linux how to look for a string in a file in python check if text is in txt python read file and search for string in python check if string contained in text file python python how to see if a string exists anywhere in afile python how to see if a strin is anywhere in a file how to check if a string is in a file python check if a file contains a string python search for a given string in a file python check if file contains string python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source