Converting a string to an integer is a common operation in programming. Sometimes, we need to convert a string to an integer for mathematical operations or storage in a database. You might also be reading in data from a file and want to convert it into a form that's easier to work with. In Python, there are several ways to accomplish this task.
Using the int() funtion
One way to convert strings to integers is by using the built-in int() function. The int() function takes in a string as an argument and returns the corresponding integer value.
The syntax is as follows:
int(string)
Example:
If we have a string '1234', calling int('1234') will return the integer 1234.
num = "1234"
print(type(num)) # Checking num datatype
number = int(num) # Coverting string num to int
print(type(number)) # Checking number datatype
print(number) # printing converted string
Returns
<class 'str'>
<class 'int'>
1234
In this example, we initialize the num variable to "1234". By checking the datatype of num, we can confirm that it is a string. We then convert the string num to int using the int() function and assign it to the number variable. Finally, we confirm the datatype of number, which is now a string, and print the converted string to the console.
If the string is not valid, then int() will throw a ValueError. For instance, int('1234a') will result in a ValueError.
num = "1234a"
print(int(num))
Returns
ValueError: invalid literal for int() with base 10: '1234a'
Using the float() funtion
We can also use the built-in float() function to convert a string to an int. The float() function takes in a string as an argument and returns the corresponding floating-point value. However, just like int(), if the string is not a valid floating-point number, float() will throw a ValueError.
The syntax is as follows:
float(string)
Example:
num = "1234"
print(float(num))
Returns 1234.0
After the conversion from string to float, we can then use the int() function to truncate the decimal and convert it to an integer.
num = "1234"
numf = float(num)
print(int(numf))
Returns 1234
Using eval() function
Finally, it's also worth mentioning that Python has a built-in eval() function, which evaluates a string as if it were a Python expression. However, the eval() function is generally not recommended because it can be dangerous (i.e., it can allow malicious code to execute).
The syntax is as follows:
eval(string)
Example:
num = "1234"
print(type(num)) # Checking num datatype
numeval = eval(num) # Coverting string num to int numeval
print(type(numeval)) # Checking numeval datatype
print(numeval) # printing converted string
Returns
<class 'str'>
<class 'int'>
1234
You can perform arithmetics using the eval() function, and it will return the integer data type.
print(eval("1234"))
print(eval("1+2+3+4"))
print(eval("1+2+3-4"))
Returns
1234
10
2
Just like the float() and int() functions, the eval() function returns Error if the string is not a valid integer.
Summary
In summary, there are several ways to convert a string to an int in Python: by using the built-in int() and float() functions or by using the literal_eval() function in the ast module. Choose a method that suits your particular situation. Thanks for reading.
Happy coding :)