In this post, we are going to explored How to Convert String to Integer in Python for example. In Python, a string can be converted to a number using the float() or int() function.
What is Python int() function?
In Python, int() is an inbuilt function that converts a given value to an integer number. The int() function takes two parameters.
Syntax
int(value=0,base=10)
- value: It is a number or string value to be converted into an integer number.
- base: It is an optional parameter, the base of the given value. Its default value is 10.
Return value:
- Returns an integer object from a number or string.
- If the value is not provided then return 0.
- if the base value is passed it handles string in the given base (0,2,8,16)
Related post
Convert Integer To String In Python
1. How to convert Python str to int
- In this example, we have variable mynum = “20” which is containing an integer value.
- We are verifying the type of the variable by using the type() function, To get the int from string by converting it to an integer by using the int() function.
mynum = "20"
print('type of number :',type(mynum))
str_to_int = int(mynum)
print('converted number type :',type(str_to_int))
Output
type of number : <class 'str'>
converted number type : <class 'int'>
2. How to convert input Python str to int?
In this example, we are taking input from the user, which is a number. The value the user entered is 20000. It looks like a number, But while checking the type using the type() method we found it is not a number, performing mathematic operation throws an error.
#Program to convert input Python str to int
salary = input("Enter is your salary:")
print(salary)
print('type of input :',type(salary))
increase_salary = salary+10000
Print('Total salary :',increase_salary)
Output
Enter is your salary:20000
20000
type of input : <class 'str'>
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\Python_program\sample.py", line 6, in <module>
increase_salary = salary+10000
TypeError: can only concatenate str (not "int") to str
To perform mathematics operation on input number,First, we are converting string type salary into integer type using int() function and performing addition.
salary = input("Enter is your salary:")
print(salary)
print('type of input :',type(salary))
salary = int(salary)
increase_salary = salary+10000
print('Total salary :',increase_salary)
Output
Enter is your salary:20000
20000
type of input : <class 'str'>
Total salary : 30000
3.Convert Python list of string to list of integer
We have a list_num containing element of number as string.We are using python int() function to convert list of string to list of integer.
#Program Convert Python list of string to list of integer
list_num = ['12','13','14','30','35']
result_list = [int(val) for val in list_num]
print('list of string to int:',result_list)
Output
list of string to int: [12, 13, 14, 30, 35]
4.Convert string to int with different base
When we convert string to int the default base of number is 10. if need to convert to a different base apart from base 10, then we need to specify the base while conversion.The base must be between 2 to 32.
#Program Convert string to int with different base
mynum = '431'
print('type of mynum :\n',type(mynum))
#convrting str to int
str_to_int = int(mynum)
print('\n str to int:\n',type(str_to_int))
print('\n converted num :\n',str_to_int)
str_to_intbase8 = int(mynum, base=8)
print('\ntype of converted mynum base 8:\n',type(str_to_int))
print('\n converted num :\n',str_to_int)
Output
type of mynum :
<class 'str'>
str to int:
<class 'int'>
converted num :
431
type of converted mynum base 8:
<class 'int'>
converted num :
431
5. Convert str to int with base 16 or 32
We are converting the string to integer with base 16 & 32.
mynum = '431'
str_to_intbase16 = int(mynum, base=16)
print('type:',type(str_to_intbase16))
print('\n converted type :\n',str_to_intbase16)
str_to_intbase32 = int(mynum, base=32)
print('\ntype:\n',type(str_to_intbase32))
print('\n converted num :\n',str_to_intbase32)
Output
type: <class 'int'>
converted type :
1073
type:
<class 'int'>
converted num :
4193
Summary:
We have explored How to Convert String to Integer in Python that includes input string, list of string to list of integer, Convert string to int with different base by using python in-built int() function.