As a Python developer, you may have to extract digits from a string multiple times in your coding journey. This situation may occur in web development and competitive coding. The possible use cases include when we need to get the numbers from the CSV file, process the user input, or get some numerical data from the textual information.

This Python blog will elaborate on several ways of extracting digits from a string.

How to Extract Digits From a Python String?

The need for digit extraction from strings/text arises when you have to perform mathematical operations on the digits that are stored as string,  Digits can be extracted from a Python string in the following ways:

  • Using Regex 
  • Using the split and append
  • Using isdigit() in a List Comprehension
  • Using the Filter() Function
  • Using NumPy

Let’s discuss how all these methods can be used to extract the digits from the Python string.

Method 1: regex

The most common method to extract the digits from the Python string is using the regex(Regular Expressions) model. Any digit or a special character can be easily verified by the re-model. The regex model is added to Python’s library. Using the findall() function, the numbers can be extracted from the string. The code below explains the regex model to extract the digits from the string:

import re
text = "Taylor Swift lucky number is 13."
digits = re.findall(r'\d+', text)
print(digits)

In the above code,

  • import re imports the regular expression model and a string variable, text is assigned with the value “Taylor Swift lucky number is 13.”
  • digits = re.findall(r’\d+’, text) uses the re.findall() function to find the digits(\d+)  in the strings
  • print(digits) prints the digits that are found in the text string

Output:

Method 2: Split and Append The Numbers To A List using split() and append()

Numbers can also be extracted from the text using split() and append() functions in a list. First, the string is split into a list using the split() function and the numbers are searched with a built-in Python function that appends the item at the end of the list. Let’s see the code to find out how we can use split() and append() functions to extract the digits from the text:

text = "We have 4 girls and 2 boys in the team."
digits = []
for part in text.split():
    if part.isdigit():
        digits.append(int(part))
print(digits)

In the above code,

  • A string text is defined with the value “We have 4 girls and 2 boys in the team.”.
  • digits= [ ] initializes an empty list that is going to store the numbers from the string text
  • for part in text.split(): starts a for loop that iterates over each word(part) in the text string. The split() function splits the text into individual words.
  • if part.isdigit(): finds out if the present word entirely composes the digits and isdigit() checks for any numeric data in the string.
  • digits.append(int(part)) if the condition is true, it means the word contains a digit. The digit will be converted to an integer with the int() function. The numeric data is appended to the digits list using the append() function.
  • print(digits) prints the list of digits that are found in the text.

Output:

Method 3: Using isdigit() Function In A List Comprehension

Using the isdigit() function in a list comprehension can also get out work done. It can search for digits in the string and store them in a list with the help of list comprehension. The isdigit() functions returns True if there is any digit found. In the other case, the value will be false. Let us look at the code to learn how we can extract digits from a list comprehension:

text = 'In the thrilling football match, the home team scored 4 goals in the first half, while the visiting team managed to score 2 goals in the second half, resulting in a 4-2 victory.'
digits = [int(new_str) for new_str in str.split(text) if new_str.isdigit()]
print(digits)

In the above code,

  • A variable text is assigned and given a string value containing the digits 4 and 2.
  • str.split(text) uses the split() function to split the text into separate words. Split divides the words based on tabs and spaces and returns a list.
  • for new_str in str.split(text) creates a for loop that iterates on each new word (new_str) in the list from the words got from split()
  • if new_str.isdigit() checks whether the string has all the numeric data or not. In this case, new_str will be checked if it consists of all the digits or not.
  • int(new_str) converts the digit in string format to an integer if the condition is true because the original digit was in the string format.
  • All the converted digits are collected into the list digits.
  • print(digits) prints the digits list showing all the numbers that are extracted from the text.

Output:

Method 4 : Using filter() function

Another way of extracting the digits from the text is using the filter() function. The digits can be extracted from the text using the lambda function. The following code explains how the filter() function can be used to get the numerical data:

text = "I need to get 3 copies of 4 sets."
result = list(filter(lambda x: x.isdigit(), text.split()))
result = [int(s) for s in result]
print(str(result))

In the above code,

  • an input string text is defined with the value “I need to get 3 copies of 4 sets.”
  • text.split() split the input string into individual words
  • The lambda function takes an argument x to check if each word consists entirely of digits by using the isdigit() method.
  • The filter function filters the text from the list with the lambda function applied to each word. It only returns the values for which the lambda function is true.
  • The list() function will convert all the filtered elements which in this case are numbers into a list
  • print(str(result)) prints the result list that contains the integers extracted from the list.

Output:

Method 5: Using Numpy

Numpy can be used to get the digits from the text. NumPy is a powerful library in Python for numerical computations in Python. Let us see how we can extracts digits using NumPy:

import numpy as np
text= "The recipe calls for 3 cups of flour and 2 cups of sugar to make delicious cookies."
y = np.array(text.split())
result = y[np.char.isnumeric(y)].astype(int)
print(str(result))

In the above code,

  • The Numpy library is imported
  • A string having the digits 3 and 2 is initialized
  • The string is first split into individual words using text.split. The resulting list is stored in a numpy array y
  • np.char.isnumeric(y) tells which elements in y are digits. The array y is indexed to extract the digits.
  • The array contains the elements that are strings. The digits in the string format are converted to integers with the help of the astype() function. Int is given as a parameter 
  • print(str(result)) prints the result that contains the extracted digits from the text.

Output:

Conclusion

The most common method used to extract the digits is the regex model. Other methods included using functions like split(), append(), isdigit(), and filter(). The numeric data can also be collected by importing the NumPy library. In this article, we have gone through various ways in which digits can be extracted from a Python string while illustrating each method with an example.