Python string to int
Converting a string to int is a fundamental programming requirement. Most of the times in our code, we get values in string format while many of those values need to be converted to an integer.
Example, a program that adds two numbers and reads those numbers as input from command line, they are read as a string in the program while for performing addition, they should be converted to an integer.
This post will show you the method to convert a string to int in python.
Python has a built-in
int()
function which takes a string as argument, converts it into an integer and returns the integer value.Example,
# read from keyboard in_value = input("Enter a value\n") print(in_value) # convert to integer in_value_int = int(in_value) print(in_value_int)
Above program reads a value as input from the keyboard and converts it to an integer.
Note that you will not see any difference when a string and int values are printed.
Look at the output of the program below
Enter a value
234
String value: 234
Int value: 234
Both string and int look same right. Now, try to add a value to both string and int and you will see the difference.
Run the below program which adds 2 to both string and integer values.
# read from keyboard in_value = input("Enter a value\n") # convert to integer in_value_int = int(in_value) # add 2 to integer value print('Int value:', in_value_int + 2) # add 2 to string value print('String value:', in_value + 2)
Above code produces following output
Enter a value
123
Int value: 125
Traceback (most recent call last):
File “test.py”, line 8, in <module>
print(‘String value:’, in_value + 2)
TypeError: must be str, not int
It adds 2 to the integer value and prints it but when adding to the actual input value(which is a string) it gives an error.
This is because you cannot add a number to string directly using + operator.
If you have a string in binary format and you want to convert it to an integer, then simply passing it to int() function will only convert it to integer format. Example,
s = '1101' i = int(s) print(i)
This will print
1101
To convert a binary string to corresponding decimal value, supply the base or radix of the string. For binary, base is 2. Example,
s = '1011' i = int(s, base=2) print(i)
This prints
11
which is binary converted to decimal or integer.
Hex string to int
For converting a hex string to integer, use int()
function with base
argument as 16, which is the base or radix of hexadecimal numbers. Example,
s = 'A' i = int(s, base=16) print(i)
which will be
10
Similarly, for octal string to int conversion, base
will be 8.
Convert decimal values
Remember that int function can convert strings which are in integer format. It will fail to parse a string having decimal in it. Thus, if you write int('23.3')
, it will throw ValueError: invalid literal for int() with base 10: ‘23.3’
For converting a decimal value to an int, precede the int function with a float function as int(float('23.3'))
which will return 23.
float()
function will first convert the decimal string into a decimal number format. Subsequently, int
function will reduce it to an integer by truncating the values after decimal.
Negative string to int
If the string value is preceded with a minus sign(-), then int()
will convert it to an int as usual and place a minus sign before it.
Thus, a string with minus sign will be converted to a negative integer as shown below
s = '-25' i = int(s) print(i)
Output will be
-25
Default value of int() function
int()
function takes a string as argument and returns its integer equivalent but if you pass nothing to this function, then it will return 0.
Thus, zero(0) is the default return value of int()
function. Example,
default = int() print("No argument to int function:", default)
Output will be
No argument to int function: 0