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
Updated on 30-Sep-2019 06:50:30

Advertisements