In Python, how can I parse a numeric string like \"545.2222\"
to its corresponding float value, 545.2222
? Or parse the string \"31\"
t
for number and char together :
string_for_int = "498 results should get"
string_for_float = "498.45645765 results should get"
first import re:
import re
#for get integer part:
print(int(re.search(r'\d+', string_for_int).group())) #498
#for get float part:
print(float(re.search(r'\d+\.\d+', string_for_float).group())) #498.45645765
for easy model :
value1 = "10"
value2 = "10.2"
print(int(value1)) #10
print(float(value2)) #10.2