How to Convert String to Float or Int in Python

This tutorial article will introduce how to convert string to float or int in Python.

float() to convert string to float in Python

float() converts the string to the float pointing number, if possible.

>>> float('111.2222')
111.2222
>>> float('111.0')
111.0
>>> float('111')
111.0

int to convert string to int in Python

int could convert the string to an integer if the given string is the representation of an integer. Otherwise it will raise ValueError.

>>> int('111')
111
>>> int('111.0')
Traceback (most recent call last):
  File "<pyshell#43>", line 1, in <module>
    int('111.0')
ValueError: invalid literal for int() with base 10: '111.0'
>>> int('111.22222')
Traceback (most recent call last):
  File "<pyshell#45>", line 1, in <module>
    int('111.22222')
ValueError: invalid literal for int() with base 10: '111.22222'

You could convert the string to float firstly and then cast it to int if the given string is the representation of a floating pointing number.

>>> int(float('111.0'))
111

ast.literal_eval to convert string to float or int in Python

ast.literal_eval(string) safely evaluate the given string containing a Python expression. It could convert the string to either float or int automatically.

>>> ast.literal_eval('111.2222')
111.2222
>>> ast.literal_eval('111.0')
111.0
>>> ast.literal_eval('111')
111

Localization and Commas in converting string to float in Python

If the comma , exists in the string representation, then the float conversion will throw an ValueError exception. But commas are regualry used as either a thousand seperator in countries like US or UK, for example 111,111.22, or decimal mark in most European countries, for example 111,222.

>>> float('111,111.22')
Traceback (most recent call last):
  File "<pyshell#54>", line 1, in <module>
    float('111,111.22')
ValueError: could not convert string to float: '111,111.22'
>>> float('111,111')
Traceback (most recent call last):
  File "<pyshell#55>", line 1, in <module>
    float('111,111')
ValueError: could not convert string to float: '111,111'

The locale module should be used to assign the locale information and then locale.atof() function could parse the given string as a float according to the locale settings.

Commas as thousand seperator in US or UK

>>> import locale
>>> A = '111,111.222'
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
'en_US.UTF-8'
>>> locale.atof(A)
111111.222
>>> locale.atof('111,111')
111111.0

The comma , could be interpreted correctly if the right locale setting is given.

Be also aware that the conversion result is always a float type even if the given string representation is an integer.

Commas as demical mark in European countries

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'nl_NL')
'nl_NL'
>>> locale.atof('111,222')
111.222
>>> locale.atof('111.111,222')
111111.222

111,222 is converted to 111.222 properly, and 111.111,222 could also be converted properly to 111111.222 as the point . is used as the thousand seperator in European countries like the Netherlands.

Comparison of performances of different methods to convert string to float or int in Python

We use timeit to check the efficiency performances between different methods.

>>> import timeint
>>> timeit.timeit('float(111.2222)', number=1000000)
0.14707240000007005
>>> timeit.timeit('ast.literal_eval("111.2222")', setup='import ast', number=1000000)
4.779956100000163
>>> timeit.timeit('locale.atof("111.2222")', setup='import locale; locale.setlocale(locale.LC_ALL, "en_US.UTF-8")', number=1000000)
6.092166299999917

ast.literal_eval() method is tremendously slower than float method as shown above. It should not be the first choice if you simply need to convert string to float or int in Python. ast.literal_eval() is over-engineered in this application as its main target should be evaluating the string containing Python expression.

locale.atof() is also extremely slow, therefore it should only be used when commas exist in the string for the convention in US or UK, or point . means the thousand seperator but comma , means the decimal mark in European countries. In other words, you should use locale.atof() if it is really necessary.

Related Articles - Python String

  • How to Check a String Is Empty in a Pythonic Way
  • How to Remove Whitespace in a String
  • How to convert string to datetime
  • How to Convert String to Lowercase in Python 2 and 3
  • How to Check Whether a String Contains Substring in Python
  • How to Convert a List to String in Python
  • How to Read One File Line by Line to A List in Python
  • How to Convert String to Bytes in Python
  • comments powered by Disqus