LearnFrenzy
Sign in Take a free test

File Handling · Predict the Output · Question 14

Characters and bytes on disk Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 14 Back to Exercise

What is the output of the following Python program?

main.py
import os, tempfile
os.chdir(tempfile.mkdtemp())

s = "café ₹"
with open("u.txt", "w", encoding="utf-8") as f:
    f.write(s)
print(len(s), len(s.encode("utf-8")), os.path.getsize("u.txt"))
  • A 6 6 6
  • B 9 9 9
  • C 6 9 6
  • D 6 9 9 Correct
Correct Answer: Option D

6 characters. In UTF-8, é takes 2 bytes and ₹ takes 3: 3 + 2 + 1 + 3 = 9 bytes, which is also the file size.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links