A text file named message.txt contains the following lines: Good morning. I…
2026
A text file named message.txt contains the following lines:
Good morning.
I will be there on time.
Consider the following Python code:
F = open('message.txt')
S1 = F.read(3)
S2 = F.readline(4)
S3 = F.read(4)
print(S1, S3, sep='@')
Which of the following will be the correct output of the above Python code?
- A.
Goo@rnin
- B.
Goo@I wi
- C.
ValueError
- D.
EOFError
Attempted by 63 students.
Show answer & explanation
Correct answer: A
File content:
Good morning.\n
I will be there on time.
S1 = F.read(3)
Reads first 3 characters → "Goo"
Pointer now at: d (in "Good...")
S2 = F.readline(4)
Reads up to 4 characters from current position → "d mo"
(Not printed, but pointer moves ahead)
Pointer now at: r (in "morning")
S3 = F.read(4)
Reads next 4 characters → "rnin"
Final print:
print(S1, S3, sep='@') → Goo@rnin