Python Program to Get Line Number in which Given Word is Present

Given a text file, the task is to get the line number in which the given word is present in Python.

Program to Get Line Number in which Given Word is Present 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.
  • Take a variable that gives the line number and initialize its value to 1.
  • 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 value of the line number.
  • Increment the line number by 1
  • 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"
# Take a variable which gives the line number and initialize its value to 1.
linenumb = 1
# Give the word as static input and store it in a variable.
word = "btechgeeks"
print('The given word {', word, '} is present in lines :')
# 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:
    # Iterate through the lines of the file using the For loop.
    print('The words in the given file : ')
    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 value of linenumber
            print(linenumb)
        # increment the linenumber by 1
        linenumb += 1

Output:

The given word { btechgeeks } is present in lines :
The words in the given file : 
1
4

Samplefile.txt:

hello this is btechgeeks
hello good morning 
python programs
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.
  • Take a variable that gives the line number and initialize its value to 1.
  • Give the word as user input using the int(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 value of the line number.
  • Increment the line number by 1
  • 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"
# Take a variable which gives the line number and initialize its value to 1.
linenumb = 1
# Give the word as user input using the int(input()) function and store it in a variable.
word = input('Enter some random to get line number of it : ')
print('The given word {', word, '} is present in lines :')
# 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:
    # Iterate through the lines of the file using the For loop.
    print('The words in the given file : ')
    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 value of linenumber
            print(linenumb)
        # increment the linenumber by 1
        linenumb += 1

Output:

Enter some random to get line number of it : python
The given word { python } is present in lines :
The words in the given file : 
3

Samplefile.txt:

hello this is btechgeeks
hello good morning 
python programs
btechgeeks

Sample Implementation in google colab: