English

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 - Computer Science (Python)

Advertisements
Advertisements

Question

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.

Short Answer
Advertisements

Solution

To solve this, open "Space.txt" in read mode and extract its content. Use a for loop to examine each character using the isdigit() method, incrementing a counter whenever a numeric value is encountered. Finally, return the accumulated total, which results in eleven for the provided example text.

shaalaa.com
  Is there an error in this question or solution?
2025-2026 (March) Set 4

APPEARS IN

Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×