I have a text file as below.
l[0]l[1]l[2]l[3]l[4]l[5]l[6]
-----------------------------------
1| abc is a book and cba too
2| xyz is a pencil and zyx too
3| de
You can check the length of the array of words.
But do note, when you index directly on l, you are going at the character level and not the word level as you seem to want to. Also, I'd use == instead of is.
Do something like this:
with open("a.txt", 'r') as fr:
for l in fr:
words = l.split()
if len(words) < 3:
continue
if words[3] == "book" or words[3] == "pencil":
print("Book or pencil")
elif words[3] == "pen":
print("Pen")