How to extract numbers from a string in Python?


There are several ways to extract numbers from a string in Python. One way to extract numbers from a string is to use regular expressions. Regular expressions are patterns that you can use to match and manipulate strings. Here's an example code snippet that uses regular expressions to extract all the numbers from a string:

Using the re.findall() function

Example

In this example, we're using the re.findall() function to search for all occurrences of numbers in the text string. The regular expression \d+\.\d+|\d+ matches both floating-point numbers and integers.

import re
text = "The price of the book is $29.99"
numbers = re.findall('\d+\.\d+|\d+', text)
print(numbers)

Output

['29.99']

Using the isdigit() method

Another way to extract numbers from a string is to use the isdigit() method, which returns True if all the characters in a string are digits.

Example

In this example, we're splitting the text string into a list of words using the split() method. We then iterate over each word and check if it's a digit using the isdigit() method. If it is, we append it to the numbers list after converting it to an integer.

text = "There are 10 apples in the basket"

numbers = []
for word in text.split():

    if word.isdigit():
        numbers.append(int(word))
print(numbers)

Output

[10]

Using a regular expression

You can use the re module in Python to extract numbers from a string using regular expressions. Here is an example:

Example

This code will output a list of numbers that appear in the string: [3, 1, 2, 3]. The regular expression '\d+' matches one or more digits in the string.

import re
string = "There are 3 numbers in this string: 1, 2, and 3."
numbers = re.findall('\d+', string)
print(numbers)

Output

['3', '1', '2', '3']

Using a loop and isdigit() method

You can loop through each character in the string and check if it's a digit using the isdigit() method.

Example

string = "There are 3 numbers in this string: 1, 2, and 3."
numbers = []
current_number = ""

for char in string:
    if char.isdigit():
        current_number += char

    elif current_number:
        numbers.append(int(current_number))
        current_number = ""

if current_number:
    numbers.append(int(current_number))

print(numbers)

Output

 [3, 1, 2, 3]

Using split() and isdigit() method

If the numbers are separated by non-digit characters, you can split the string using those characters and then check each resulting substring to see if it's a number.

Example

string = "There are 3 numbers in this string: 1, 2, and 3."
numbers = []
for substring in string.split():
    if substring.isdigit():
        numbers.append(int(substring))
print(numbers)

Output

[3]

Using the isnumeric() method and a for loop

Example

This code creates an empty list called numbers, then splits the input string into a list of words. It then loops through each word in the list and checks if it is a number using the isnumeric() method. If it is a number, it is appended to the numbers list. Finally, the numbers list is printed.

my_string = "I have 2 apples and 3 oranges"
numbers = []
for word in my_string.split():
    if word.isnumeric():
        numbers.append(int(word))
print(numbers)

Output

[2, 3]

Using the re.findall() function

Example

This code imports the re module and uses the re.findall() function to find all instances of digits in the input string. The resulting list of digit strings is then converted to a list of integers using a list comprehension. Finally, the numbers list is printed.

import re
my_string = "I have 2 apples and 3 oranges"
numbers = [int(num) for num in re.findall(r'\d+', my_string)]
print(numbers)

Output

[2, 3]

Using a generator expression and the map() function:

Example

This code uses a generator expression and the map() function to create a list of integers. The generator expression loops through each word in the input string and only returns the ones that are digits using the isdigit() method. The map() function applies the int() function to each digit string, converting it to an integer. Finally, the resulting list of integers is printed.

my_string = "I have 2 apples and 3 oranges"
numbers = list(map(int, (word for word in my_string.split() if word.isdigit())))
print(numbers)

Output

[2, 3]

Updated on: 10-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements