easy +6 pts

Extract digits from string

Return all digit characters from a string as a single string, preserving order.

Write a function `extract_digits(s: str) -> str` that takes a single string `s` and returns a new string containing only the digit characters (0-9) from `s`, in the same order they appear. Non-digit characters are ignored. If there are no digits, return an empty string. - The input string may contain letters, spaces, punctuation, and digits. - The function should handle empty strings correctly. - Do not use any external libraries.

Constraints

- `s` is a string with length 0 to 10,000. - The function should return a string consisting only of digit characters.

Example

>>> extract_digits("Order 1234: 5 apples")
'12345'
>>> extract_digits("abc!?")
''
>>> extract_digits("")
''
>>> extract_digits("A1B2C3")
'123'
6 points ~8 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Iterate over each character in the string and check if it is a digit.
Use `str.isdigit()` to test each character.
Collect the digits into a list and join them at the end.
An empty string input should return an empty string.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.