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

PythonServer Side ProgrammingProgramming

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
raja
Published on 12-Dec-2017 11:39:30
Advertisements