Python - Extract digits from given string Last Updated : 28 Jan, 2025 Comments Improve Suggest changes Like Article Like Report We need to extract the digit from the given string. For example we are given a string s=""abc123def456gh789" we need to extract all the numbers from the string so the output for the given string will become "123456789" In this article we will show different ways to extract digits from a string in Python methods like using regular expressions and basic string manipulation to get only the numbers from a string.Using str.isdigit() str.isdigit() method in Python is used to check if all characters in a string are digits. It returns True if all characters are digits (0-9) and there is at least one character otherwise, it returns False. Python s = "abc123def456gh789" # Extract digits using a loop and isdigit() digits = ''.join(char for char in s if char.isdigit()) print(digits) Output123456789 Explanation: We loop through each character in the string s and uses isdigit() to check if a character is a digit and only the digits are joined together into a new string which is printed as the result.Using Regular Expressions (re.findall)Using re.findall() in Python allows to extract all substrings that match a specific pattern such as digits, from a given string. Python import re s = "abc123def456gh789" # Extract digits using regex digits = ''.join(re.findall(r'\d', s)) print(digits) Output123456789 Explanation:re.findall() function is used to find all digits (\d) in the string, returning them as a list of individual characters.''.join() method combines the list of digits into a single string for the final resultUsing str.translate() with str.maketrans()str.translate() method combined with str.maketrans(), provides an efficient way to replace multiple characters or remove unwanted characters from a string in Python. Python s = "abc123def456gh789" # Extract digits using translate and maketrans digits = s.translate(str.maketrans('', '', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()')) print(digits) Output123456789 Explanation:str.maketrans('', '', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()') creates a translation table that maps all lowercase and uppercase letters, as well as special characters, to None, effectively removing them from the string.s.translate() applies the translation table to the string s, removing all characters except the digits resulting in a string that contains only the numeric characters. Comment More infoAdvertise with us Next Article Python - Extract digits from given string M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python - Extract Rear K digits from Numbers Given an Integer list, extract rear K digits from it. Input : test_list = [5645, 23567, 34543, 87652, 2342], K = 2 Output : [45, 67, 43, 52, 42] Explanation : Last 2 digits extracted. Input : test_list = [5645, 23567, 34543, 87652, 2342], K = 4 Output : [5645, 3567, 4543, 7652, 2342] Explanation : L 5 min read Python - Extract Percentages from String Given a String, extract all the numbers that are percentages. Input : test_str = 'geeksforgeeks 20% is 100% way to get 200% success' Output : ['20%', '100%', '200%'] Explanation : 20%, 100% and 200% are percentages present. Input : test_str = 'geeksforgeeks is way to get success' Output : [] Explana 2 min read Python - Extract Strings with a digit Given a Strings List, extract those with atleast one digit. Input : test_list = ['gf4g', 'is', 'best', 'gee1ks'] Output : ['gf4g', 'gee1ks'] Explanation : 4, 1 are respective digits in string. Input : test_list = ['gf4g', 'is', 'best', 'geeks'] Output : ['gf4g'] Explanation : 4 is digit in string. M 5 min read Python - Extract numbers from list of strings We are given a list of string we need to extract numbers from the list of string. For example we are given a list of strings s = ['apple 123', 'banana 456', 'cherry 789'] we need to extract numbers from the string in list so that output becomes [123, 456, 789].Using Regular ExpressionsThis method us 2 min read Python - Extract K length substrings The task is to extract all possible substrings of a specific length, k. This problem involves identifying and retrieving those substrings in an efficient way. Let's explore various methods to extract substrings of length k from a given string in PythonUsing List Comprehension List comprehension is t 2 min read Like