How to parse a string to float or int in python?



To parse a string to int, you can use the following:

try:
    print int('112')
except ValueError:
    print 'Cannot parse'

This will give you the output:

112

To parse a string to float, you can use the following:

try:
    print float('112.15')
except ValueError:
    print 'Cannot parse'

This will give you the output:

112.15

Advertisements