Write Python code that opens 'input.txt', goes to the 5th line, then selects the 5th word in that line, and prints the word along with its length.
💡 Model Answer
You can read the file line by line and index into the list. Example: with open('input.txt', 'r') as f: lines = f.readlines() if len(lines) < 5: raise ValueError('File has fewer than 5 lines') line = lines[4].strip() words = line.split() if len(words) < 5: raise ValueError('5th line has fewer than 5 words') word = words[4] print(f"Word: {word}") print(f"Length: {len(word)}") This code uses 0‑based indexing: lines[4] is the 5th line, words[4] is the 5th word. It handles missing lines or words by raising an error. Complexity is O(n) for reading the file, but only a few lines are processed after reading. If the file is large, you could iterate until the 5th line to avoid loading the entire file into memory.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500