Home / Programs / Read line number 4 from the following file
🚀 Programming Example

Read line number 4 from the following file

👁 276 Views
💻 Practical Program
📘 Step Learning

Write a python program which will read line number 4 from the following file

📌 Information & Algorithm

Given Input: test.txt file: Create a test.txt file and add the below content to it.

line1
line2
line3
line4
line5
line6
line7

Expected Output:


💻 Program Code

# read file
with open("test.txt", "r") as fp:
    # read all lines from a file
    lines = fp.readlines()
    # get line number 3
    print(lines[2])
                        
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.