Python - Retain Numbers in String Last Updated : 16 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Retaining numbers in a string involves extracting only the numeric characters while ignoring non-numeric ones.Using List Comprehensionlist comprehension can efficiently iterate through each character in the string, check if it is a digit using the isdigit() method and join the digits together to form a new string . Python s= 'G4g is No. 1 for Geeks 7' # Retain Numbers in String res = "".join([ele for ele in s if ele.isdigit()]) print(str(res)) Output417 Let's understand different method to retain numbers in string.Table of ContentUsing Regular Expressions Using filter()Using reduce()Using map()Using loopUsing Regular Expressions This method uses regular expressions to find all digits in the string. It's concise and optimal when dealing with large input strings as regex engines are highly optimized. Python import re s= 'G4g is No. 1 for Geeks 7' # Retain Numbers in String res = re.sub(r'[^\d]+', '',s) print(str(res)) Output417 Using filter()By using filter(), we can iterate over the characters of the string and applying isdigit() to check each character and retain only the numeric characters. Then, by usingjoin(), we can combine the filtered characters into a single string as the result. Python s = 'G4g is No. 1 for Geeks 7' res = ''.join(filter(str.isdigit,s)) print(str(res)) Output417 Using reduce()This method uses reduce() to iteratively build the result string by checking if each character in the string is a digit. If it is, the character is added to the result. Python from functools import reduce s = 'G4g is No. 1 for Geeks 7' res = reduce(lambda x, y: x + y if y.isdigit() else x,s, '') print(str(res)) Output417 Using map()map() applies the isdigit() check to each character in the string and we replace non-digit characters with an empty string. The result is then joined into a string. Python s = 'G4g is No. 1 for Geeks 7' res = ''.join(map(lambda x: x if x.isdigit() else '', s)) print(str(res)) Output417 Using loopThis method involves iterating over each character in the string and checking if it is a digit then building the result string by adding the digits together. Python s= 'G4g is No. 1 for Geeks 7' res="" digits="0123456789" for i in s: if i in digits: res+=i print(str(res)) Output417 Comment More infoAdvertise with us Next Article Python - Retain Numbers in String M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python - Splitting Text and Number in string Given a string containing both letters and numbers, the task is to separate the text (letters) and the numbers into two separate outputs. For example, if the input is "abc123", the output should be "abc" and "123". Let's discuss various ways in which we can achieve this in Python.Using for loopThis 3 min read Python | Sort Numerical Records in String Sometimes, while working with Python records we can have a problem that they may occur in name and number format in strings. These may be required to be sorted. This problem can occur in many domains in which data is involved. Let us discuss certain ways in which this task can be performed. Method # 6 min read Insert a number in string - Python We are given a string and a number, and our task is to insert the number into the string. This can be useful when generating dynamic messages, formatting output, or constructing data strings. For example, if we have a number like 42 and a string like "The number is", then the output will be "The num 2 min read Python - Replace all numbers by K in given String We need to write a python program to replace all numeric digits (0-9) in a given string with the character 'K', while keeping all non-numeric characters unchanged. For example we are given a string s="hello123world456" we need to replace all numbers by K in the given string so the string becomes "he 2 min read Frequency of Numbers in String - Python We are given a string and we have to determine how many numeric characters (digits) are present in the given string. For example: "Hello123World456" has 6 numeric characters (1, 2, 3, 4, 5, 6).Using re.findall() re.findall() function from the re module is a powerful tool that can be used to match sp 3 min read Like