Extract Decimal Numbers from a String in Python



To extract decimal numbers from a string in Python, regular expressions are used. A regular expression is a group of characters that allows you to use a search pattern to find a string or a set of strings. RegEx is another name for regular expressions.

The re module in Python is used to work with regular expressions.

In this article, we will learn how to extract decimal numbers from a string in Python using regular expressions. To retrieve non-digit characters from a string, we use the following regular expression -

 \d+\.\d+

Where,

  • \d returns a match where the string contains digits (numbers from 0 to 9)

  • + implies zero or more occurrences of characters.

  • \ signals a special sequence (can also be used to escape special characters).

  • . is any character (except the newline character).

Using findall() function

In the following example, let us assume 'Today's temperature is 40.5 degrees' as a string. Here, we need to extract the decimal number 40.5 from the string.

Example

The following is an example code through which the decimals are extracted from a string in Python. We begin by importing the regular expression module. Then, we have used the findall() function, which is imported from the re module.

import re
string = "Today's temperature is 40.5 degrees."
x=re.findall(r"\d+\.\d+", string)
print(x)

The re.findall() function returns a list containing all matches, that is list of strings with non-digits.

On executing the above code snippet, the following output is obtained.

['40.5']

Using finditer() function

In the following example, let us assume 'The score was 99.5 out of 100.0.' as a string. Here, we need to extract the decimal numbers 99.5 and 100.0 from the string.

Example

The following is an example code through which the decimals are extracted from a string in Python. We begin by importing the regular expression module. Then, we have used finditer() function, which is imported from the re module.

import re

text = "The score was 99.5 out of 100.0."
matches = re.finditer(r"\d+\.\d+", text)

for match in matches:
   print(match.group())

The re.finditer() function returns an iterator yielding match objects, which allows for better control over the matches found in the string.

When you run the program, it will show this output -

99.5
100.0

Using findall() function with float conversion

In the following example, let us assume 'Prices: Rs.10.50, Rs.20.75, and Rs.5.99 only.' as a string. Here, we need to extract decimal numbers 10.50, 20.75, and 5.99 from the string.

Example

The following is an example code through which the decimals are extracted from a string in Python. We begin by importing the regular expression module. Then, we have used findall() function along with a list comprehension for conversion to float.

import re

text = "Prices: Rs.10.50, Rs.20.75, and Rs.5.99 only."
numbers = re.findall(r"\d+\.\d+", text)
decimal_numbers = [float(num) for num in numbers]
print(decimal_numbers)

After running the program, you will get this result -

[10.5, 20.75, 5.99]

Using a loop and split() method

In the following example, let us assume 'Speed: 60.5 km/h, Distance: 120.25 km.' as a string. Here, we need to extract decimal numbers 60.5 and 120.25 from the string.

Example

The following is an example code through which the decimals are extracted from a string in Python. We will use basic string methods by splitting the string and attempting to convert each part to float.

text = "Speed: 60.5 km/h, Distance: 120.25 km"
parts = text.split()
decimals = []

for part in parts:
    try:
        number = float(part)
        decimals.append(number)
    except ValueError:
        pass

print(decimals)

Following si the output of the above program -

[60.5, 120.25]

Using re for negative numbers and optional digits

In the following example, let us assume 'Loss: -0.5%, Gain: .75%, Neutral: 0.0%.' as a string. Here, we need to extract decimal numbers including negative values -0.5, 0.0, and .75.

Example

The following is an example code through which the decimals are extracted from a string in Python. We import the regular expression module and utilize findall() with a pattern that matches negative numbers as well.

import re

text = "Loss: -0.5%, Gain: .75%, Neutral: 0.0%"
result = re.findall(r"-?\d*\.\d+", text)
print(result)

You will see this result after executing the program -

['-0.5', '.75', '0.0']
Updated on: 2025-04-23T12:50:17+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements