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.
'Hello, I am Romy\nI am a content writer at GfG\nNice to meet you\nHello, hii all fine'
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']
1, 4 Hello is present in 1st and 4th line.