मराठी

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

प्रश्न

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.

लघु उत्तर
Advertisements

उत्तर

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
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
2025-2026 (March) Set 4

APPEARS IN

Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×