How to convert a String to an equivalent integer value with int() and float() functions?


A program receives an argument (e.g from the command line entered the age of a person) that is supposed to be in the integer context but the format is a string. You need to convert that string into an equivalent integer value. In this tutorial, we will show how we can create an integer value from the string?

By default, python can not cast a string into an integer.

What will happen if try to type Cast from string to integer?

If you try to typecast the type of integer variable will change from integer to string.

age = "123";
 print(type(age));
 x = int(5);
 print(type(x));
 x = age; 
 print(type(x));
 print("x = ", x); 
 x = x + 1;
 print("x = ", x);

Output->

<class 'str'>
<class 'int'>
<class 'str'>
x =  123
 Traceback (most recent call last):
   File "StringtoInteger.py", line 11, in 
     x = x + 1;
 TypeError: can only concatenate str (not "int") to str

How we can convert a string to integer(int)?

We can use the inbuilt function int() to convert a string into an integer. The function has the following syntax.

int(inputString, base) -
inputStrinng – it is the string that needs to convert into an integer.
base – The base argument is an integer value. It represents the number format. For example, for decimal, it is 10, for binary 2, and for hexadecimal, it is 16. What base we should use depends on the input string format. If a programmer use int() function with the wrong base, there will error message invalid literal for int() with base baseValue: ‘inputString‘. It is an optional parameter the default is 10.

Following is an example of converting strings in various formats into the integer using function int().

stringValue = "1111";
 #Decimal string
 intValue = int(stringValue,10);
 #Check the type of output value.
 print(type(intValue)); 
 print("String  = ", stringValue);
 print("Converted Integer (base 10)  = ", intValue);
 stringValue = "10101";
 #Binary String
 intValue = int(stringValue,2);
 print("String  = ", stringValue);
 print("Converted Integer (base 2)  = ", intValue);
 stringValue = "1a";
 #Hexadecimal String
 intValue = int(stringValue,16);
 print("String  = ", stringValue);
 print("Converted Integer (base 16)  = ", intValue);

Output->

<class 'int'>
 String  =  1111
 Converted Integer (base 10)  =  1111
 String  =  10101
 Converted Integer (base 2)  =  21
 String  =  1a
 Converted Integer (base 16)  =  26 

How to convert a String into an integer with decimal values?

Till now we have seen how to convert a string into an integer. But what if the string has a decimal part such as 24.3. Will the int() function will work? The answer is No, to convert a string with decimal values we need an inbuilt function float(). The function works in the following way.

float(StringValue) - Returns a floating-point or an integer value.

StringValue – The string that needs to pass for the conversion.

Following is an example for conversion with float() function.

stringValue = "2.3";

 intValue = float(stringValue);
 #Check the type of output value.
 print(type(intValue)); 
 print("String  = ", stringValue);
 print("Converted Integer  = ", intValue);
 stringValue = "2";
 
 intValue = float(stringValue);
 print("String  = ", stringValue);
 print("Converted Integer  = ", intValue);

Output->

<class 'float'> 
 String  =  2.3
 Converted Integer  =  2.3
 String  =  2
 Converted Integer  =  2.0

We can see that the float() function can work on integer (base 10) and strings also.