How to obtain the line number in which given word is present using Python?

To obtain the line number from the file where the given word is present, create a list in which each index contains the content of each line. To do so follow the below instruction.

First, we need a file to read from. So, create a file inside Jupiter notebook using the magic function as shown below:

%%writefile geeks.txt 
Hello, I am Romy 
I am a content writer at GfG 
Nice to meet you 
Hello, hii all fine

Or you can use any .txt file.
 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# READ FILE
df = open("geeks.txt")
  
# read file
read = df.read()
  
# return cursor to
# the beginning
# of the file.
df.seek(0)
read

chevron_right


Output:



‘Hello, I am Romy\nI am a content writer at GfG\nNice to meet you\nHello, hii all fine’

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# create empty list
arr = []
  
# count number of
# lines in the file
line = 1
for word in read:
    if word == '\n':
        line += 1
print("Number of lines in file is: ", line)
  
for i in range(line):
    # readline() method,
    # reads one line at
    # a time
    arr.append(df.readline())

chevron_right


Output: 

Number of lines in file is: 4
['Hello, I am Romy\n',
'I am a content writer at GfG\n', 
'Nice to meet you\n',
'Hello, hii all fine']

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Function that will return
# line in which word is present
def findline(word):
    for i in range(len(arr)):
        if word in arr[i]:
            print(i+1, end=", ")
  
  
findline("Hello")

chevron_right


Output: 

1, 4
Hello is presnt in 1st and 4th line.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.