Q32
3 marksShort AnswerSection C

(a) Write a Python function that counts and returns the number of digits appearing in the text file "Space.txt". For example, if the file contains :

``

Space exploration has unlocked incredible

advancements in technology and science. Since the

first moon landing in 1969, space agencies have sent

probes to Mars, Jupiter and beyond. The ISS,

orbiting Earth at about 400 km, serves as a hub for

research. With missions planned for 2030, humanity's

cosmic journey continues!

`

Then the function should return 11.

OR

(b) Write a Python function that displays the words in which the lowercase letter 'e' appears at least twice in the text file 'Space.txt'. For example, if the file contains :

`

Space exploration has unlocked incredible

advancements in technology and science. Since the

first moon landing in 1969, space agencies have sent

probes to Mars, Jupiter and beyond. The ISS,

orbiting Earth at about 400 km, serves as a hub for

research. With missions planned for 2030, humanity's

cosmic journey continues!

``

Then the function should display : agencies serves incredible advancements science. research.

File Handling in Python
Text File Handling in Python
Official Answer

Both parts require reading the entire content of a text file and processing it character-by-character or word-by-word.


(a) Count digits in the file:

``python

def CountDigits():

count = 0

f = open("Space.txt", "r")

data = f.read()

f.close()

for ch in data:

if ch.isdigit():

count += 1

return count

`

For the given file, the digits are '1969' (4 digits), '400' (3 digits), and '2030' (4 digits), giving a total of 11, matching the expected output.


(b) Display words where lowercase 'e' occurs at least twice:

`python

def DisplayWords():

f = open("Space.txt", "r")

data = f.read()

f.close()

words = data.split()

for word in words:

if word.count('e') >= 2:

print(word, end=' ')

`

This correctly prints: agencies serves incredible advancements science. research.` — matching the expected output.

file handlingread()isdigit()split()count()text file processingSpace.txt

Marking Scheme

  • 11 mark: correctly opening and reading the file 'Space.txt' (open() with 'r' mode and read()/readlines()).
  • 21 mark: correct logic to identify digits using isdigit() (for a) OR correct logic to split into words and count 'e' occurrences (for b).
  • 31 mark: correct accumulation/printing and correct return value (for a) or correct print statement producing space-separated words (for b).

Hint

Use f.read() to load the whole file as one string; for (a) use ch.isdigit() in a loop; for (b) use data.split() and word.count('e') >= 2.

Quick Oral Answer

I read the whole file using read(), then for part (a) I loop through every character and count digits using isdigit(), and for part (b) I split the content into words and print only those whose word.count('e') is at least 2.

Analysis & Explanation

This question tests the ability to open, read, and process an entire text file using Python's file handling constructs.


Concept — reading the file

open("Space.txt","r") opens the file in read mode, and .read() loads its entire content as one string, including newline characters — this is essential since the digits/words may be spread across multiple lines.


Concept — part (a), counting digits

The built-in string method ch.isdigit() returns True only for characters '0'–'9', so iterating over every character in the file content and counting digit characters correctly tallies all numeric characters regardless of which 'number' they belong to (i.e., 1969 contributes 4 individual digit characters, not 1).


Concept — part (b), word filtering by letter frequency

data.split() breaks the file content into a list of whitespace-separated words (including any trailing punctuation attached to a word, such as 'science.'). word.count('e') counts how many times the exact lowercase 'e' appears in that word; only words with 2 or more occurrences are printed.


Exam trap

A common error is closing the file before reading it, or using readline() instead of read()/readlines(), which processes only the first line and misses digits/words on later lines. For part (b), students often forget the case sensitivity requirement — the question explicitly asks for lowercase 'e', so using .lower() before counting would wrongly include words that only contain uppercase 'E'.

Common Mistakes

  1. 1Using readline() instead of read() or readlines(), which processes only the first line of the file and misses digits/words on subsequent lines.
  2. 2Forgetting to close the file after reading, or not opening it in read mode explicitly.
  3. 3For part (b), using word.lower().count('e') instead of word.count('e'), which incorrectly matches words containing only uppercase 'E' as well, violating the 'lowercase e' condition.

Interesting Facts

Python's str.isdigit() considers not only ASCII digits '0'-'9' but also many Unicode digit characters from other scripts, making it more inclusive than a manual check against '0123456789'.

Using data.split() with no arguments automatically treats any sequence of whitespace (spaces, tabs, newlines) as a single delimiter, which is why it correctly separates words even across the multiple lines of the Space.txt file.

Spotted a mistake or something unclear?

Tell us — we fix reported answers fast.

Frequently Asked Questions

Why use read() instead of readline() for this problem?

read() loads the entire file content as a single string in one go, ensuring every character and word across all lines is processed; readline() only reads one line at a time and would miss data on subsequent lines if not called repeatedly.

Why does the digit count treat 1969 as 4 digits instead of 1 number?

The function iterates character by character and checks each individual character with isdigit(), so a number like 1969 contributes four separate digit characters (1, 9, 6, 9) to the total count, not one.