How to Extract All Digits From a String in Python etd_admin, December 22, 2025December 22, 2025 When you work with user input, filenames, logs, or messy text, you’ll often need to pull out only the numbers. This article shows simple, reliable ways to do that, including cases where you want digits as a string, as a list, or as an integer. If you searched extract all digits from a string in Python, the best approach depends on what you want the output to look like (joined digits, separate digits, or grouped numbers). Option 1: Use str.isdigit() (simple and beginner-friendly) This is the easiest method: loop through characters and keep only digits. Join digits into one string: s = "Order #A19B-204X" digits = "".join(ch for ch in s if ch.isdigit()) print(digits) # 19204s = "Order #A19B-204X" digits = "".join(ch for ch in s if ch.isdigit()) print(digits) # 19204 Get digits as a list: s = "Room 4B, floor 12" digits_list = [ch for ch in s if ch.isdigit()] print(digits_list) # ['4', '1', '2']s = "Room 4B, floor 12" digits_list = [ch for ch in s if ch.isdigit()] print(digits_list) # ['4', '1', '2'] Why it’s nice: No imports Easy to read Great for “just digits” extraction Option 2: Use Regular Expressions (re) (compact and powerful) Regex is great when you want a clean, one-liner, or when patterns get more complex. Join all digits into one string: import re s = "User: id=77, code=AB-908" digits = "".join(re.findall(r"\d", s)) print(digits) # 77908import re s = "User: id=77, code=AB-908" digits = "".join(re.findall(r"\d", s)) print(digits) # 77908 Find grouped numbers (e.g., “77” and “908” separately): import re s = "User: id=77, code=AB-908" numbers = re.findall(r"\d+", s) print(numbers) # ['77', '908']import re s = "User: id=77, code=AB-908" numbers = re.findall(r"\d+", s) print(numbers) # ['77', '908'] Use \d for single digits, and \d+ for multi-digit number groups. If your goal is extract all digits from a string in Python, re.findall() is one of the most common solutions because it’s short and flexible. Python PythonStrings