Python Program to Print Lines Containing Given String in File

Given a string and file, the task is to print the lines that contain the given string in Python.

Program to Print Lines Containing Given String in File in Python

Method #1: Using For loop (Static Input)

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Give the word as static input and store it in a variable.
  • Iterate through the lines of the file using the For loop.
  • Split the words of the line using the split() function and store them in a variable(it is of type list).
  • Check if the given word is present in the above words using if and in keywords
  • If it is true then print the line.
  • The Exit of the Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Give the word as static input and store it in a variable.
word = "BTechGeeks"
print('Printing the lines that are containing the given word {', word, '} :')
# Open the file in read-only mode. In this case, we're simply reading the contents of the file.
with open(givenFilename, 'r') as givenfilecontent:
    for gvnfileline in givenfilecontent:
      # Split the words of the line using the split() function and store them in a variable(it is of type list).
        gvnfilewords = gvnfileline.split()
        # check if the given word is present in the above words using if and in keywords
        if word in gvnfilewords:
            # If it is true then print the line.
            print(gvnfileline)

Output:

Printing the lines that are containing the given word { BTechGeeks } :
Hello this is BTechGeeks
Good morning this is BTechGeeks

Samplefile.txt:

Hello this is BTechGeeks
good morning
python programming articles
for BTech students
who are interested in coding 
articles and Theory concepts
Good morning this is BTechGeeks

Method #2: Using For loop (User Input)

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Give the word as user input using the input() function and store it in a variable.
  • Iterate through the lines of the file using the For loop.
  • Split the words of the line using the split() function and store them in a variable(it is of type list).
  • Check if the given word is present in the above words using if and in keywords
  • If it is true then print the line.
  • The Exit of the Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Give the word as user input using the input() function and store it in a variable.
gvnword = input('Enter some random word :')
print('Printing the lines that are containing the given word {',word,'} :')
# Open the file in read-only mode. In this case, we're simply reading the contents of the file.
with open(givenFilename, 'r') as givenfilecontent:
    for gvnfileline in givenfilecontent:
      # Split the words of the line using the split() function and store them in a variable(it is of type list).
        gvnfilewords = gvnfileline.split()
        # check if the given word is present in the above words using if and in keywords
        if gvnword in gvnfilewords:
            #If it is true then print the line.
            print(gvnfileline)

Output:

Enter some random word :morning
Printing the lines that are containing the given word { BTechGeeks } :
good morning
Good morning this is BTechGeeks

Samplefile.txt:

Hello this is BTechGeeks
good morning
python programming articles
for BTech students
who are interested in coding 
articles and Theory concepts
Good morning this is BTechGeeks

Sample Implementation in google colab: