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:

string_to_be_converted="2.3456"
print("Before conversion:",string_to_be_converted, type(string_to_be_converted))
after_conversion=float(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:

x="23.2"
y="23.1"
print(x-y)
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.

x="23.2"
y="23.1"
print(float(x)-float(y))
0.09999999999999787

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.

x="23.2a"
print(float(x))
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.

x="23"
print(float(x))
23.0

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:

string_to_be_converted="2"
print("Before conversion:",string_to_be_converted, type(string_to_be_converted))
after_conversion=int(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:

x="23"
y="23"
print(x+y)
2323

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.

x="23"
y="23"
print(int(x)+int(y))
46

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:

x="23.0"
print(int(x))
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.