How to extract a float from a string in Python

1 Answer

0 votes
import re

text = "The price is 148.95 dollars"
    
float_regex = r"[-+]?\d*\.\d+|\d+"
match = re.search(float_regex, text)
    
if match:
    number = float(match.group())
    print("Extracted float:", number)



'''
run:

Extracted float: 148.95

'''

 



answered Jul 29, 2025 by avibootz
...