Skip to main content

How to extract numbers from a string in Python

How to extract numbers from a string in Python.

Here is a step-by-step tutorial on how to extract numbers from a string in Python.

Step 1: Import the necessary modules

First, you need to import the regular expression module re. This module provides functions for pattern matching with regular expressions.

import re

Step 2: Define the string

Next, you need to define the string from which you want to extract the numbers.

string = "I have 10 apples and 5 oranges."

Step 3: Use regular expressions to extract numbers

Now, you can use regular expressions to extract the numbers from the string. The re.findall() function can be used to find all occurrences of a pattern in the string.

numbers = re.findall(r'\d+', string)

In this example, the regular expression r'\d+' is used. \d matches any digit (0-9), and + matches one or more occurrences of the preceding pattern. So, this pattern will match any sequence of one or more digits.

Step 4: Process the extracted numbers

Finally, you can process the extracted numbers as per your requirement. Here, we will print each number on a separate line.

for number in numbers:
print(number)

This will output:

10
5

Additional Examples

Example 1: Extracting decimal numbers

If you want to extract decimal numbers, you can modify the regular expression pattern to include the decimal point.

string = "The price is $9.99."
numbers = re.findall(r'\d+\.\d+', string)

In this example, the pattern r'\d+\.\d+' will match any sequence of one or more digits, followed by a decimal point, and then one or more digits.

Example 2: Extracting negative numbers

To extract negative numbers, you can modify the regular expression pattern to include the negative sign.

string = "The temperature is -10 degrees Celsius."
numbers = re.findall(r'-\d+', string)

In this example, the pattern r'-\d+' will match a negative sign followed by one or more digits.

Example 3: Extracting numbers with commas

If the numbers in your string contain commas as thousands separators, you can modify the regular expression pattern to handle them.

string = "The population is 1,234,567."
numbers = re.findall(r'\d{1,3}(?:,\d{3})*', string)

In this example, the pattern r'\d{1,3}(?:,\d{3})*' will match one to three digits, followed by zero or more occurrences of a comma and three digits. The (?:,\d{3})* is a non-capturing group that allows repetition.

That's it! You now know how to extract numbers from a string in Python using regular expressions.