Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
Regular Expression Pattern
To retrieve decimal numbers 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 one or more occurrences of characters
\. matches a literal dot character (escaped with backslash)
Using findall() Function
The findall() function returns a list containing all decimal matches in the string ?
import re string = "Today's temperature is 40.5 degrees." decimals = re.findall(r"\d+\.\d+", string) print(decimals)
['40.5']
Using finditer() Function
The finditer() function returns an iterator yielding match objects, which allows for better control over the matches ?
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())
99.5 100.0
Converting to Float Numbers
You can convert the extracted strings to actual float numbers using list comprehension ?
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)
[10.5, 20.75, 5.99]
Using split() Method Without Regex
An alternative approach uses 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)
[60.5, 120.25]
Handling Negative Numbers and Optional Digits
For more complex patterns including negative numbers and decimals starting with a dot, use this enhanced pattern ?
import re text = "Loss: -0.5%, Gain: .75%, Neutral: 0.0%" result = re.findall(r"-?\d*\.\d+", text) print(result)
['-0.5', '.75', '0.0']
Comparison
| Method | Returns | Best For |
|---|---|---|
findall() |
List of strings | Simple extraction |
finditer() |
Iterator of match objects | Position information needed |
split() + try/except |
List of floats | No regex dependency |
Conclusion
Use re.findall() with the pattern r"\d+\.\d+" for basic decimal extraction. For negative numbers or decimals without leading digits, use r"-?\d*\.\d+" pattern.
