easy +5 pts

Extract Digits Only

Pull every digit from a messy string and return it as an integer.

Write a function `extract_digits(s: str) -> int` that takes a string `s` and returns a new integer formed by concatenating every digit character (0-9) that appears in `s`, in the order they appear. If there are no digits, return 0. The input may contain uppercase and lowercase letters, punctuation, spaces, and newlines. Leading zeros in the extracted digit sequence should be handled normally: when converting to an integer, leading zeros are dropped (e.g., `"0a1"` gives `1`). However, if the digit sequence is exactly `"0"` or consists only of zeros, the result should be `0`.

Constraints

- `s` is a string of length 0 to 1000. - `s` may contain any printable ASCII characters. - Time complexity: O(n), where n is the length of `s`. - Space complexity: O(n) for the digit collection.

Example

```python
>>> extract_digits("a1b2c3")
123
>>> extract_digits("No digits here!")
0
>>> extract_digits(" 42 is the answer 42")
4242
>>> extract_digits("000")
0
```
5 points ~5 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Loop through each character and check if it is a digit using `char.isdigit()`.
Collect the digit characters into a list or string, then join them.
Use `int()` on the joined string; if the string is empty, return 0.
Remember that `int("0")` is 0, so leading zeros are automatically handled.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.