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.
