How to convert string to int in python

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.

String to int conversion
Python has a built-in int function which takes a string as argument, converts it into an integer and returns the integer value.

# read from keyboard
in_value = input("Enter a value\n")
# convert to integer
in_value_int = int(in_value)

Above program reads a value as an 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
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
Int value: 125
Traceback (most recent call last):
File “”, 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.

Converting 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.
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, 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

Hope you liked this post. Keep visiting!!!


Mark Your Impression

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.