How to extract all float numbers from string in Python

1 Answer

0 votes
import re

s = 'a213python 34 java98 c++ 88397 php 3.14 swift 6-89x'
   
lst = [float(temp) for temp in re.findall(r'-?\d+\.?\d*', s)]
   
print(lst)
     
     
     
'''
run:
     
[213.0, 34.0, 98.0, 88397.0, 3.14, 6.0, -89.0]

'''

 



answered Apr 12, 2021 by avibootz
...