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 entering a person’s age) that is supposed to be in the integer context, but the format is a string. The program needs to convert that string into an equivalent integer value for a computation.
This tutorial will show how to create an equivalent integer value from a string.
By default, python can not cast a string into an integer.
Let’s see if the typecasting is sufficient only.
Typecasting does the implicit conversion from one type to another. In the following example, we will see whether it can be used to convert a string to an integer.
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);
<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
In the above example, the x is of type string, and the x = x + 1 does it failed to convert x into an integer from a string.
How can we convert a string to an 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
– A base argument represents the number format. It is an optional parameter. The default value is 10.
For example, for decimal, it is 10. For binary 2, and for hexadecimal, it is 16. Depending on the format of the input string, we should use a different base value. If a programmer use int() function with the wrong base, there will be an error message invalid literal for int() with base baseValue
: ‘inputString
‘.
Following is an example of converting strings in various formats into the integer using the 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);
<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.
The following is an example of conversion with the 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 integers (base 10) and strings also.