Convert numeric string to float/int in Python

You must be familiar with Python data types and their role in categorizing data along with guiding the compiler on how the programmer intends to use the data.

One can store the numerical values under different types like integer, float, double, string etc in Python.
However, sometimes the programmer might want to change the data type of the number.

Say, for example, you want to concatenate two numbers. You will not be able to concatenate the two numbers unless they are both under the same data type.
You can even consider the example of reading the user input from the files, in which case, the input will be stored as a string only.

Through this tutorial, you will understand how to convert your numeric string data values into floating-point or integer values in Python.
Before diving into the tutorial, you might want to check out, Data Types in Python!

The float() function

It takes a string or a number as the input and returns a floating-point number from it.

Syntax: float(numeric value/string to be converted to float)

Let us look into some examples to understand better!

Example 1: Conversion of numeric string to float:

print("Before conversion:",string_to_be_converted, type(string_to_be_converted))
print("After conversion:",after_conversion,type(after_conversion))
Before conversion: 2.3456 <class 'str'>
After conversion: 2.3456 <class 'float'>


Example 2: Subtraction of two numeric string values:

TypeError                                 Traceback (most recent call last)
<ipython-input-8-6c1b61ccd551> in <module>
      1 x="23.2"
      2 y="23.1"
----> 3 x-y

TypeError: unsupported operand type(s) for -: 'str' and 'str'

As you can see, since mathematical operations can’t be performed on strings, we convert them to float.


You might notice that there is a slight difference in the resulting value. However, this inaccuracy is due to math issues and has nothing to do with the conversion.

Example 3: If you pass a non-numeric value to the float() method, it throws an exception.

ValueError                                Traceback (most recent call last)
<ipython-input-16-2dee874760ab> in <module>
      1 x="23a"
----> 2 print(float(x))

ValueError: could not convert string to float: '23a'

However, it doesn’t throw any exception in the case of non-floating point values, as long as they are numbers.


The int() function

It takes a string or a number as the input and returns an integer number from it.
Syntax: int(numeric value/string to be converted to int)

Looking into some examples for the same:

Example 1: Conversion of numeric string to float:

print("Before conversion:",string_to_be_converted, type(string_to_be_converted))
print("After conversion:",after_conversion,type(after_conversion))
Before conversion: 2 <class 'str'>
After conversion: 2 <class 'int'>

Example 2: Addition of two numeric string values:


You must observe that in the above example, instead of adding the two numbers, the compiler concatenates them since they are strings.
Thus, we convert them into integers in order to add them up.


You know by now that the float() method accepts non-floating-point values too, as long as they are numbers.
However, the int() method accepts only integer values as input.
You can observe the same below:

Example 3:

ValueError                                Traceback (most recent call last)
<ipython-input-33-137bb3ac91fe> in <module>
      1 x="23.0"
----> 2 print(int(x))

ValueError: invalid literal for int() with base 10: '23.0'


You can also read,Type conversion in Python!

Leave a Reply

Your email address will not be published. Required fields are marked *