{"id":26202,"date":"2021-12-21T09:28:20","date_gmt":"2021-12-21T03:58:20","guid":{"rendered":"https:\/\/python-programs.com\/?p=26202"},"modified":"2021-12-21T09:28:20","modified_gmt":"2021-12-21T03:58:20","slug":"python-program-to-extract-digits-from-a-string-2-easy-ways","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-extract-digits-from-a-string-2-easy-ways\/","title":{"rendered":"Python Program to Extract Digits from a String – 2 Easy Ways"},"content":{"rendered":"
When working with strings, we frequently run into the problem of needing to get all of the numerical occurrences. This type of issue is common in competitive programming as well as online development. Let’s solve the issue now!.<\/p>\n
1) Using Python isdigit() Function:<\/strong><\/p>\n If the given string contains digit characters, the Python isdigit()<\/strong> method returns True.<\/p>\n Syntax:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Using List comprehension:<\/strong><\/p>\n Output:<\/strong><\/p>\n 3)Using regex Library:<\/strong><\/p>\n The Python regular expressions library, known as the regex library,’ allows us to detect the presence of specific characters in a string, such as numbers, special characters, and so on. r’\\d+’\u00a0 – to extract numbers from the string<\/p>\n ‘\\d+’ helps the findall() function in identifying the existence of any digit.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 1) Using Python isdigit() Function:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Using List comprehension:<\/strong><\/p>\n Output:<\/strong><\/p>\n 3)Using regex Library:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n When working with strings, we frequently run into the problem of needing to get all of the numerical occurrences. This type of issue is common in competitive programming as well as online development. Let’s solve the issue now!. Program to Extract Digits from a String – 2 Easy Ways in Python Using Built-in Functions (Static …<\/p>\nstring.isdigit()<\/pre>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_strng = \"678_Goodmorning 123 hello all\"\r\nprint(\"The Given string = \", gvn_strng)\r\n# Take a variable and initialize it with an empty string.\r\nnew_str = \"\"\r\n# Iterate in the given string using the for loop.\r\nfor chrctr in gvn_strng:\r\n # Inside the for loop, check if the character in a given string is a digit\r\n # or not using the isdigit() function and if conditional statement.\r\n if chrctr.isdigit():\r\n # If it is true, then concatenate the character to the above declared empty\r\n # string using the '+' operator and store it in the same variable.\r\n new_str = new_str + chrctr\r\n# Print all the digits from a given string.\r\nprint(\"The digits present in a given string = \", new_str)\r\n<\/pre>\n
The Given string = 678_Goodmorning 123 hello all\r\nThe digits present in a given string = 678123<\/pre>\n
# Give the string as static input and store it in a variable.\r\ngvn_strng = \"678_Goodmorning 123 hello all\"\r\n# Print the given string\r\nprint(\"The Given string = \", gvn_strng)\r\n# Using list comprehension to get all the digits present in a given string\r\nnew_lst = [int(chrctr) for chrctr in gvn_strng if chrctr.isdigit()]\r\n# Print all the digits from a given string.\r\nprint(\"The digits present in a given string = \", new_lst)\r\n<\/pre>\n
The Given string = 678_Goodmorning 123 hello all\r\nThe digits present in a given string = [6, 7, 8, 1, 2, 3]<\/pre>\n
\nBefore proceeding, import the regex library into the Python environment.<\/p>\nimport re<\/pre>\n
\n
# Import regex library using the import keyword.\r\nimport re\r\n# Give the string as static input and store it in a variable.\r\ngvn_strng = \"6 Goodmorning 17 hello all\"\r\n# Print the given string\r\nprint(\"The Given string = \", gvn_strng)\r\n# Pass r'\\d+'\u00a0 and given string as arguments to the re.findall() function to\r\n# extract numbers from the string and store it in another variable.\r\n# Here '\\d+' helps the findall() function in identifying the existence of any digit.\r\nrslt_digts = re.findall(r'\\d+', gvn_strng)\r\n# Print all the digits from a given string.\r\nprint(rslt_digts)\r\n<\/pre>\n
The Given string = 6 Goodmorning 17 hello all\r\n['6', '17']<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_strng = input(\"Enter some random string = \")\r\nprint(\"The Given string = \", gvn_strng)\r\n# Take a variable and initialize it with an empty string.\r\nnew_str = \"\"\r\n# Iterate in the given string using the for loop.\r\nfor chrctr in gvn_strng:\r\n # Inside the for loop, check if the character in a given string is a digit\r\n # or not using the isdigit() function and if conditional statement.\r\n if chrctr.isdigit():\r\n # If it is true, then concatenate the character to the above declared empty\r\n # string using the '+' operator and store it in the same variable.\r\n new_str = new_str + chrctr\r\n# Print all the digits from a given string.\r\nprint(\"The digits present in a given string = \", new_str)\r\n<\/pre>\n
Enter some random string = welcome6477 to Python-programs\r\nThe Given string = welcome6477 to Python-programs\r\nThe digits present in a given string = 6477<\/pre>\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_strng = input(\"Enter some random string = \")\r\n# Print the given string\r\nprint(\"The Given string = \", gvn_strng)\r\n# Using list comprehension to get all the digits present in a given string\r\nnew_lst = [int(chrctr) for chrctr in gvn_strng if chrctr.isdigit()]\r\n# Print all the digits from a given string.\r\nprint(\"The digits present in a given string = \", new_lst)\r\n<\/pre>\n
Enter some random string = 65 hello 231 all\r\nThe Given string = 65 hello 231 all\r\nThe digits present in a given string = [6, 5, 2, 3, 1]<\/pre>\n
\n
# Import regex library using the import keyword.\r\nimport re\r\n# Give the string as user input using the input() function and store it in a variable.\r\ngvn_strng = input(\"Enter some random string = \")\r\n# Print the given string\r\nprint(\"The Given string = \", gvn_strng)\r\n# Pass r'\\d+'\u00a0 and given string as arguments to the re.findall() function to\r\n# extract numbers from the string and store it in another variable.\r\n# Here '\\d+' helps the findall() function in identifying the existence of any digit.\r\nrslt_digts = re.findall(r'\\d+', gvn_strng)\r\n# Print all the digits from a given string.\r\nprint(rslt_digts)\r\n<\/pre>\n
Enter some random string = hello this is python 35 program\r\nThe Given string = hello this is python 35 program\r\n['35']<\/pre>\n","protected":false},"excerpt":{"rendered":"