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

Updated on: 30-Sep-2019

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements