Python Program to Extract Digits from a String – 2 Easy Ways

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

Method #1: Using Built-in Functions (Static Input)

1) Using Python isdigit() Function:

If the given string contains digit characters, the Python isdigit() method returns True.

Syntax:

string.isdigit()

Approach:

  • Give the string as static input and store it in a variable.
  • Take a variable and initialize it with an empty string.
  • Iterate in the given string using the for loop.
  • Inside the for loop, check if the character in a given string is a digit or not using the isdigit() function and if conditional statement.
  •  If it is true, then concatenate the character to the above declared empty string using the ‘+’ operator and store it in the same variable.
  • Print all the digits from a given string.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_strng = "678_Goodmorning 123 hello all"
print("The Given string = ", gvn_strng)
# Take a variable and initialize it with an empty string.
new_str = ""
# Iterate in the given string using the for loop.
for chrctr in gvn_strng:
    # Inside the for loop, check if the character in a given string is a digit
        # or not using the isdigit() function and if conditional statement.
    if chrctr.isdigit():
        # If it is true, then concatenate the character to the above declared empty
        # string using the '+' operator and store it in the same variable.
        new_str = new_str + chrctr
# Print all the digits from a given string.
print("The digits present in a given string = ", new_str)

Output:

The Given string =  678_Goodmorning 123 hello all
The digits present in a given string =  678123

2)Using List comprehension:

# Give the string as static input and store it in a variable.
gvn_strng = "678_Goodmorning 123 hello all"
# Print the given string
print("The Given string = ", gvn_strng)
# Using list comprehension to get all the digits present in a given string
new_lst = [int(chrctr) for chrctr in gvn_strng if chrctr.isdigit()]
# Print all the digits from a given string.
print("The digits present in a given string = ", new_lst)

Output:

The Given string =  678_Goodmorning 123 hello all
The digits present in a given string =  [6, 7, 8, 1, 2, 3]

3)Using regex Library:

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.
Before proceeding, import the regex library into the Python environment.

import re

r’\d+’  – to extract numbers from the string

‘\d+’ helps the findall() function in identifying the existence of any digit.

Approach:

  • Import regex library using the import keyword.
  • Give the string as static input and store it in a variable.
  • Print the given string.
  • Pass r’\d+’  and given string as arguments to the re.findall() function to extract numbers from the string and store it in another variable.
  • Here ‘\d+’ helps the findall() function in identifying the existence of any digit.
  • Print all the digits from a given string.
  • The Exit of the Program.

Below is the implementation:

# Import regex library using the import keyword.
import re
# Give the string as static input and store it in a variable.
gvn_strng = "6 Goodmorning 17 hello all"
# Print the given string
print("The Given string = ", gvn_strng)
# Pass r'\d+'  and given string as arguments to the re.findall() function to
# extract numbers from the string and store it in another variable.
# Here '\d+' helps the findall() function in identifying the existence of any digit.
rslt_digts = re.findall(r'\d+', gvn_strng)
# Print all the digits from a given string.
print(rslt_digts)

Output:

The Given string = 6 Goodmorning 17 hello all
['6', '17']

Method #2: Using Built-in Functions (User Input)

1) Using Python isdigit() Function:

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Take a variable and initialize it with an empty string.
  • Iterate in the given string using the for loop.
  • Inside the for loop, check if the character in a given string is a digit or not using the isdigit() function and if conditional statement.
  •  If it is true, then concatenate the character to the above declared empty string using the ‘+’ operator and store it in the same variable.
  • Print all the digits from a given string.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_strng = input("Enter some random string = ")
print("The Given string = ", gvn_strng)
# Take a variable and initialize it with an empty string.
new_str = ""
# Iterate in the given string using the for loop.
for chrctr in gvn_strng:
    # Inside the for loop, check if the character in a given string is a digit
        # or not using the isdigit() function and if conditional statement.
    if chrctr.isdigit():
        # If it is true, then concatenate the character to the above declared empty
        # string using the '+' operator and store it in the same variable.
        new_str = new_str + chrctr
# Print all the digits from a given string.
print("The digits present in a given string = ", new_str)

Output:

Enter some random string = welcome6477 to Python-programs
The Given string = welcome6477 to Python-programs
The digits present in a given string = 6477

2)Using List comprehension:

# Give the string as user input using the input() function and store it in a variable.
gvn_strng = input("Enter some random string = ")
# Print the given string
print("The Given string = ", gvn_strng)
# Using list comprehension to get all the digits present in a given string
new_lst = [int(chrctr) for chrctr in gvn_strng if chrctr.isdigit()]
# Print all the digits from a given string.
print("The digits present in a given string = ", new_lst)

Output:

Enter some random string = 65 hello 231 all
The Given string = 65 hello 231 all
The digits present in a given string = [6, 5, 2, 3, 1]

3)Using regex Library:

Approach:

  • Import regex library using the import keyword.
  • Give the string as static input and store it in a variable.
  • Print the given string.
  • Pass r’\d+’  and given string as arguments to the re.findall() function to extract numbers from the string and store it in another variable.
  • Here ‘\d+’ helps the findall() function in identifying the existence of any digit.
  • Print all the digits from a given string.
  • The Exit of the Program.

Below is the implementation:

# Import regex library using the import keyword.
import re
# Give the string as user input using the input() function and store it in a variable.
gvn_strng = input("Enter some random string = ")
# Print the given string
print("The Given string = ", gvn_strng)
# Pass r'\d+'  and given string as arguments to the re.findall() function to
# extract numbers from the string and store it in another variable.
# Here '\d+' helps the findall() function in identifying the existence of any digit.
rslt_digts = re.findall(r'\d+', gvn_strng)
# Print all the digits from a given string.
print(rslt_digts)

Output:

Enter some random string = hello this is python 35 program
The Given string = hello this is python 35 program
['35']